<?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)) . '">' . "\r\n"); //ENT_IGNORE方能显示日文 _ls_xml($sub_path,$count); fwrite($outputFileHandle,'</folder>' . "\r\n"); } } /* * 遍历文件夹中的文件 */ 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)) . '" />' . "\r\n"); } elseif (!is_dir($sub_path)) { //既不是文件也不是文件夹 echo 'failed:' . $sub_path . "\r\n"; } } } /** * 包装函数 * @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"?>' . "\r\n" //输出xml头 . '<!-- ' . htmlspecialchars($fullpath,'GB2312') . ' 列表 -->' . "\r\n" //输出列表文件夹名 . '<list>' . "\r\n" //根头 ); $count[0] = 0; //统计文件夹数量 $count[1] = 0; //统计文件数量 ob_start(); //捕捉不正常的文件 //调用内核函数 _ls_xml($fullpath,$count); //输出不正常的文件信息作为备注 fwrite($outputFileHandle,'<remarks>' . htmlspecialchars(ob_get_contents(),'GB2312') . '</remarks>' . "\r\n"); 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,因为回车有\r\n //fwrite(STDOUT,"done"); } todo();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。