加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

文件夹列表,xml格式

发布时间:2020-12-16 08:09:15 所属栏目:百科 来源:网络整理
导读:?php/** GBK* 本文件功能:文件夹列表,xml格式*//*** 内核函数* @param string $path 全路径* @param resource $outputFileHandle* @param array $count* @return void*/function _ls_xml($path,$outputFileHandle,$count) { //打开文件夹句柄,有些文件夹
<?php
/*
* GBK
* 本文件功能:文件夹列表,xml格式
*/

/**
* 内核函数
* @param <string> $path 全路径
* @param <resource> $outputFileHandle
* @param <array> $count
* @return <void>
*/
function _ls_xml($path,$outputFileHandle,&$count) {
  //打开文件夹句柄,有些文件夹例如System Volume Information打不开
  $pathHandle = opendir($path);
  if (!$pathHandle)
    return;
  /*
  * 遍历文件夹中的文件夹
  */
  while (($file = readdir($pathHandle)) !== false) {    //readdir()返回打开目录句柄中的一个条目
    $sub_path = $path . DIRECTORY_SEPARATOR . $file;   //构建子路径
    if ($file == '.' || $file == '..') {   //过滤上级目录
      continue;
    } else if (is_dir($sub_path)) {            //如果是文件夹那么递归
      $count[0]++;
      fwrite($outputFileHandle,'<folder path="' . htmlspecialchars($sub_path,ENT_IGNORE,'GB2312') . '" createTime="' . date('Y-m-d H:i:s',filectime($path)) . '">' . "rn");  //ENT_IGNORE方能显示日文
      _ls_xml($sub_path,$count);
      fwrite($outputFileHandle,'</folder>' . "rn");
    }
  }
  /*
  * 遍历文件夹中的文件
  */
  rewind($pathHandle);
  while (($file = readdir($pathHandle)) !== false) {    //readdir()返回打开目录句柄中的一个条目
    $sub_path = $path . DIRECTORY_SEPARATOR . $file;   //构建子路径
    if (is_file($sub_path)) {//如果是文件那么输出
      $count[1]++;
      fwrite($outputFileHandle,'<file name="' . htmlspecialchars($file,'GB2312') . '" size="' . number_format(filesize($sub_path)) . '" createTime="' . date("Y-m-d H:i:s",filectime($sub_path)) . '" />' . "rn");
    } elseif (!is_dir($sub_path)) { //既不是文件也不是文件夹
      echo 'failed:' . $sub_path . "rn";
    }
  }
}

/**
* 包装函数
* @param <type> $fullpath
*/
function ls($fullpath) {
  /*
  * 整理参数
  */
  $fullpath = str_replace('/',DIRECTORY_SEPARATOR,$fullpath);
  if (substr($fullpath,-1) == DIRECTORY_SEPARATOR)
    $fullpath = substr($fullpath,strlen($fullpath) - 1);
  /*
  * 异常
  */
  if (!is_dir($fullpath)) {
    echo $fullpath . '不是文件夹';
    return;
  }
  /*
  * 包装内核函数
  */
  $outputFile = str_replace(':','',$fullpath);               //准备输出目标文件
  $outputFile = str_replace(DIRECTORY_SEPARATOR,'-',$outputFile);
  $outputFile .= '.' . date('Y-m-d His');
  $outputFileHandle = fopen($outputFile,'w+');               //打开输出目标文件
  fwrite($outputFileHandle,'<?xml version="1.0" encoding="gbk"?>' . "rn" //输出xml头
      . '<!-- ' . htmlspecialchars($fullpath,'GB2312') . ' 列表 -->' . "rn" //输出列表文件夹名
      . '<list>' . "rn" //根头
  );
  $count[0] = 0; //统计文件夹数量
  $count[1] = 0; //统计文件数量
  ob_start();   //捕捉不正常的文件
  //调用内核函数
  _ls_xml($fullpath,$count);
  //输出不正常的文件信息作为备注
  fwrite($outputFileHandle,'<remarks>' . htmlspecialchars(ob_get_contents(),'GB2312') . '</remarks>' . "rn");
  ob_end_flush();
  fwrite($outputFileHandle,'</list>'); //根尾
  fclose($outputFileHandle);                         //关闭目标文件
  $outputFileDone = basename($outputFile) . '.[' . $count[0] . '][' . $count[1] . '].xml';
  rename($outputFile,$outputFileDone);
  echo $outputFileDone . ' created';
}

/**
* 界面函数,CLI
*/
function todo() {
  fwrite(STDOUT,'输入目录:');
  ls(trim(fgets(STDIN)));     //必须trim,因为回车有rn
  //fwrite(STDOUT,"done");
}

todo();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读