PHP文件与目录操作的方法

时间:2025-10-02 23:56:08 php语言

PHP文件与目录操作的方法

  主要介绍了PHP文件与目录操作,涉及php针对文件与目录的遍历、判断与排序相关操作技巧,注释中备有较为详细的说明,需要的朋友可以参考下。

  本文实例讲述了PHP文件与目录操作。分享给大家供大家参考,具体如下:

  文件目录相关函数

  <?php

  /pic/p>

  function outputcurfiles ($allowedtypes, $thedir){

  /pic/p>

  if (is_dir ($thedir)){

  /pic/p>

  $scanarray = scandir ($thedir);

  /pic/p>

  /pic/p>

  /pic/p>

  for ($i = 0; $i < count ($scanarray); $i++){

  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){

  /pic/p>

  if (is_file ($thedir . "/" . $scanarray[$i])){

  /pic/p>

  /pic/p>

  if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){

  /pic/p>

  $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);

  if (in_array ($thepath['extension'], $allowedtypes)){

  /pic/p>

  echo $scanarray[$i] . "<br />";

  }

  }

  }

  }

  }

  } else {

  echo "对不起,这个目录不存在.";

  }

  }

  $allowedtypes = array ("txt","html");

  outputcurfiles ($allowedtypes, "testfolder");

  /pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/

  function recurdir ($thedir) {

  /pic/p>

  try {

  if ($adir = opendir ($thedir)){

  /pic/p>

  while (false !== ($anitem = readdir ($adir))){

  /pic/p>

  if ($anitem != "." && $anitem != ".."){

  /pic/p>

  /pic/p>

  if (is_dir ($thedir . "/" . $anitem)){

  ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php

  ?><P style="margin-left: 10px;" mce_style="margin-left:10px;"><?php

  recurdir ($thedir . "/" . $anitem );

  ?></P><?php

  } elseif (is_file ($thedir . "/" . $anitem)){

  /pic/p>

  echo $anitem . "<br />";

  }

  }

  }

  } else {

  throw new exception ("Sorry, directory could not be openend.");

  }

  } catch (exception $e) {

  echo $e->getmessage();

  }

  }

  echo "<br />/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/<br /><br />";

  recurdir("testfolder");

  /pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/p>

  echo "<br />/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/<br /><br />";

  function sortfilesbydate ($thedir){

  /pic/p>

  if (is_dir ($thedir)){

  /pic/p>

  $scanarray = scandir ($thedir);

  $finalarray = array();

  /pic/p>

  /pic/p>

  /pic/p>

  for ($i = 0; $i < count ($scanarray); $i++){

  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){

  /pic/p>

  if (is_file ($thedir . "/" . $scanarray[$i])){

  /pic/p>

  $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);

  }

  }

  }

  /pic/p>

  asort ($finalarray);

  return ($finalarray);

  } else {

  echo "对不起,这个目录不存在.";

  }

  }

  /pic/p>

  $sortedarray = sortfilesbydate ("testfolder");

  /pic/p>

  while ($element = each ($sortedarray)){

  echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";

  }

  ?>

【PHP文件与目录操作的方法】相关文章:

php中目录文件操作详谈02-21

PHP遍历目录文件常用方法03-06

php操作excel文件的方法小结12-02

PHP开发:linux 父目录权限影响子目录文件操作02-13

PHP文件怎么操作01-29

php查找指定目录下指定大小文件的方法09-11

PHP常用的文件操作函数10-24

php文件操作函数解释02-09

用PHP遍历目录下的全部文件03-17