您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> php编程 >> 正文

用PHP实现文件管理系统

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   

  /**

  * @]Class Name[= IO

  * @]Class URI[= System.IO

  * @]Purpose[=

  * 本类用于对文件系统的处理

  * @]Author[= SNakeVil <51JS,BU,PHPx> ([email protected])

  * @]Version[= 1.1.1

  * @]Create[= 17:13 2004-3-25

  * @]Modifications[=

  * 4:04 2004-3-30 "

  * + 修复 generate_path() 方法中存在的一些 BUG

  * + 重新设计方法 no_comment()

  * 4:32 2004-3-29

  * + 简化方法 list_dir() 的返回值[

  * + 增加方法 file_info() 获取文件或目录信息

  * 5:35 2004-3-28

  * + 整理优化算法

  * 7:31 2004-3-27

  * + 将错误处理抽象为基类 pB

  * + 增加方法 no_comment() 删除文件中 C 规范注释

  * @]See[=

  */

  class IO extends SnkClass

  var $result; // 操作返回结果,如方法返回值为 mixed,则成功操作结果可在此获得

  var $exec_cmd; // 执行方法,暂时没应用到

  var $exist_dir; // 创建目录时最后存在的目录,现用于 copy() 和 move()

  var $buffer_size; // 文件读取缓冲区大小,根据服务应用规模和服务器配置修改,建议默认值

  function IO()

  parent::SnkClass();

  $this->result = array();

  $this->exec_cmd = "";

  $this->exist_dir = "";

  $this->buffer_size = 8192;

  return $this;

  }

  /**

  * @]Method Name[= list_dir()

  * @]Purpose[=

  * 读取指定目录内容,返回内容数组

  * @]Parameter[=

  * string $dir_path 指定目录路径,默认为当前目录

  * @]Return[= mixed 错误返回 FALSE,否则返回

  * array(

  * array("name","location","type"),

  * ......

  * )

  * @]Author[= SNakeVil <51JS,BU,PHPx> ([email protected])

  * @]See[=

  */

  function list_dir($path=".")

  if (!is_dir($path)) return $this->error_occur(0x000B, __FUNCTION__);

  if (!is_readable($path)) return $this->error_occur(0x0002, $path);

  $dh = @opendir($path);

  $result = array();

  $path = realpath($path);

  if ($path[strlen($path)-1]!=DIRECTORY_SEPARATOR) $path .= DIRECTORY_SEPARATOR; // 保证目录绝对地址后带目录分隔符

  while (FALSE!==($fh=readdir($dh))) { // 使用 !== 防止处理名称为 0 或 FALSE 的文件、目录

  if ($fh=="."||$fh=="..") continue; // 忽略系统特定文件夹

  $i = $path.$fh; // 获取绝对地址

  $t = array(

  "name" => $fh, V u,

  "location" => $i,

  "type" => is_file($i) ? 1 : (is_dir($i) ? 0 : -1)

  );

  $result[] = $t;

  }

  closedir($dh);

  unset($dh, $fh, $t, $i);

  clearstatcache(); // 清除文件系统缓存

  return $this->result = $result;

  }

  /**

  * @]Method Name[= file_info()

  * @]Purpose[=

  * 获取指定文件或目录的属性

  * @]Parameter[=

  * string $dir_path 指定目录路径,默认为当前目录

  * @]Return[= mixed 错误返回 FALSE,否则返回

  * array("name","location","type","size","access","change","modify","read","write"),

  * @]Author[= SNakeVil <51JS,BU,PHPx> ([email protected])

  * @]See[=

  */

  function file_info($path=".")

  $path = realpath($path);

  if (!$path) return $this->error_occur(0x000A, __FUNCTION__);

  $result = array(

  "name" => substr($path, strrpos($path, DIRECTORY_SEPARATOR)+1),

  "location" => $path, FHHC,~|5

  "type" => is_file($path) ? 1 : (is_dir($path) ? 0 : -1),

  "size" => filesize($path),

  "access" => fileatime($path),

  "modify" => filemtime($path),

  "change" => filectime($path),

  "read" => is_readable($path),

  "write" => is_writeable($path)

  );

  clearstatcache();

  return $this->result = $result;

  }

  /**

  * @]Method Name[= seek file()

  * @]Purpose[=

  * 根据正则表达式条件,在相应目录及给定层次的子目录中搜索匹配的文件、目录

  * @]Parameter[=

  * string $pattern 兼容 PERL 标准的正则表达式指明搜索匹配要求,会添加 /^ $/,默认为 .*

  * string $path 进行搜索的目录路径,默认为当前路径

  * enum $seesk_type 有 -1 0 1 三种可能值,0 仅文件夹,1 仅文件,-1 两者都包括,默认为 1

  * int $sub_dir 搜索的子目录深度,指定目录不算,建议不要超过 5,默认为

  * limit $limit 搜索结果限制,避免过度浪费系统资源,默认为 100

  * @]Return[= mixed 错误返回 FALSE,否则

  * array(

  * array(

  * "name","locate","type"

  * ),

  * ......

  * )

  * @]Author[= SNakeVil <51JS,BU,PHPx> ([email protected])

  * @]See[=

  */

  function seek_file($pattern=".*", $path=".", $seek_type=1, $sub_dir_level=0, $limit=100)

  /* 检查参数值 */

  $is_error = $seek_type!=1 && $seek_type!=0 && $seek_type!=-1;

  $is_error = $is_error && (!is_int($sub_dir_level) || $sub_dir_level < 0);

  $is_error = $is_error && (!is_int($limit) || $limit < 1);

  if ($is_error) return $this->error_occur(0x000B, __FUNCTION__);

  unset($is_error);

  $result = array();

  /* array() == FALSE,所以需要使用 === */

  if (FALSE===$i=$this->list_dir($path)) return FALSE; // 如果不能列举目录,返回

  for ($j=0,$k=count($i);$j<$k;$j++) {

  if ($i[$j]["type"]==-1) continue; // 对于非目录非文件项目,跳过

  if ($i[$j]["type"]==0&&$sub_dir_level) { // 如果需要搜索下层目录

  if (FALSE===$l=$this->seek_file($pattern,$i[$j]["location"],$seek_type,($sub_dir_level - 1),$limit)) return FALSE;

  $result = array_merge($result, $l); // 将下层目录搜索结果添加

  }

  if ($seek_type+$i[$j]["type"]==1||!preg_match("/^".$pattern."$/", $i[$j]["name"])) continue; // 如果不搜索当前类型,跳过

  $result[] = $i[$j];

  if (count($result)>=$limit) { // 截去超过要求的长度,离开列举

  array_splice($result, $limit);

  break;

  }

  }

  unset($i, $j, $k, $l);

  return $this->result = $result;

  }

  /**

  * @]Method Name[= delete()

  * @]Purpose[=

  * 删除指定对象,文件或文件夹——包括内含子目录和文件的非空文件夹

  * @]Parameter[=

  * string $path 指定要删除的内容路径,文件或目录均可

  * @]Return[= boolean 错误返回 FALSE,否则 TRUE

  * @]Author[= SNakeVil <51JS,BU,PHPx> ([email protected])

  * @]See[=

  */

  function delete($path="") {

  $path = realpath($path);

  if (!$path) return $this->error_occur(0x000A, __FUNCTION__);

  if (!is_dir($path)) {

  if (@unlink($path)) return TRUE; // 文件删除成功

  return $this->error_occur(0x0004, $path);

  } else {

  if (FALSE===$i=$this->list_dir($path)) return FALSE; // 不能列举目录

  for ($j=0,$k=count($i);$j<$k;$j++)

  if (!$this