文件夹列表,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();