文件夹列表,xml格式
来源:互联网 发布:剑三成女军娘捏脸数据 编辑:程序博客网 时间:2024/06/05 04:11
<?php/* * UTF-8 * 本文件功能:文件夹列表,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(iconv('GBK', 'UTF-8', $sub_path), ENT_IGNORE, 'UTF-8') . '" createTime="' . date('Y-m-d H:i:s', filectime($path)) . '">' . "\r\n"); //ENT_IGNORE方能显示日文 _ls_xml($sub_path, $outputFileHandle, $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(iconv('GBK', 'UTF-8', $file), ENT_IGNORE, 'UTF-8') . '" 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, 0, 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="utf-8"?>' . "\r\n" //输出xml头 . '<!-- ' . htmlspecialchars(iconv('GBK', 'UTF-8', $fullpath), ENT_IGNORE, 'UTF-8') . ' 列表 -->' . "\r\n" //输出列表文件夹名 . '<list>' . "\r\n" //根头 ); $count[0] = 0; //统计文件夹数量 $count[1] = 0; //统计文件数量 ob_start(); //捕捉不正常的文件 //调用内核函数 _ls_xml($fullpath, $outputFileHandle, $count); //输出不正常的文件信息作为备注 fwrite($outputFileHandle, '<remarks>' . htmlspecialchars(iconv('GBK', 'UTF-8', ob_get_contents()), ENT_IGNORE, 'UTF-8') . '</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, iconv('UTF-8', 'GBK', '输入目录:')); ls(trim(fgets(STDIN))); //必须trim,因为回车有\r\n //fwrite(STDOUT, "done");}todo();
阅读全文
0 0
- 文件夹列表,xml格式
- xml格式
- XML 格式
- XML格式
- xml 格式
- xml格式
- XML格式
- xml格式
- 生成文件夹文件列表。
- sfo获取文件夹列表
- 在.net 中 将线性结构数据列表转换为 XML 格式的层次结构
- Android 从web应用获取xml格式的视频资讯并列表显示在屏幕上
- 【格式】项目问题列表
- 文档格式签名列表
- 自定义列表格式
- Qt获取文件夹下文件夹,文件列表
- php文件操作,文件夹列表,文件列表
- PHP获取文件夹列表,文件列表
- Java EE中验证码功能的实现讲解
- 均方误差、平方差、方差、均方差、协方差
- %lld 与 %I64d 的区别
- 1
- leetcode
- 文件夹列表,xml格式
- Codeforces Round #380~#400 div2 总结
- java在调试模式下打断点的时候,断点显示为禁用状态的解决
- 提高PHP代码执行效率小结
- hdu 1233 堆优化prim
- 对海康28181摄像头PS流解码的支持(一)
- codeforces contest 13 problem E(分块)
- 基于R语言的Kaggle案例分析学习笔记(五)
- 【入门】Java登录注册