php高效遍历文件夹、高效读取文件

/**
 * PHP高效遍历文件夹(大量文件不会卡死)
 * @param string $path 目录路径
 * @param integer $level 目录深度
 */
function fn_scandir($path = './', $level = 0) {
    $file = new FilesystemIterator($path);
    $filename = '';
    $prefix = '';
    $url = '';
    foreach ($file as $fileinfo) {
        $filename = $fileinfo->getFilename();
        $filepath = $path . $filename;
        $prefix = $level > 0 ? ('|' . str_repeat('--', $level)) : '';
        if ($fileinfo->isDir()) {
            $filepath = $filepath . '/';
            $url = '<a title="[dir] '.$filepath.'" href="?path=' . $filepath . '">' . $filename . '</a> [<a title="delete" href="?path=' . $filepath . '&action=del" onclick="return confirm(\'您确定要删除吗?\')">x</a>]';
            echo '<strong>' . $prefix . $url . '/</strong>' . '<br />';
        } else {
            $url = '<a title="[file] '.$filepath.'" href="?path=' . $filepath . '">' . $filename . '</a> [<a title="delete" href="?path=' . $filepath . '&action=del" onclick="return confirm(\'您确定要删除吗?\')">x</a>]';
            echo $prefix . $url . '<br />';
        }
        if ($fileinfo->isDir()) {
            fn_scandir($filepath, $level + 1);
        }
    }
}

/**
 * 删除非空目录里面所有文件和子目录
 * @param string $dir
 * @return boolean
 */
function fn_rmdir($dir) {
    //先删除目录下的文件:
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . "/" . $file;
            if (is_dir($fullpath)) {
                fn_rmdir($fullpath);
            } else {
                unlink($fullpath);
            }
        }
    }
    closedir($dh);
    //删除当前文件夹:
    if (rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

/**
 * PHP高效读取文件
 * @param string $filepath
 * @return string
 */
function fn_tail($filepath) {
    if (file_exists($filepath)) {
        $fp = fopen($filepath, "r");
        $str = "";
        $buffer = 1024; //每次读取 1024 字节
        while (!feof($fp)) {//循环读取,直至读取完整个文件
            $str .= fread($fp, $buffer);
        }
        return $str;
    }
}

/**
 * PHP高效写入文件(支持并发)
 * @param string $filepath
 * @param string $content
 */
function fn_write($filepath, $content) {
    if ($fp = fopen($filepath, 'a')) {
        $startTime = microtime();
        // 对文件进行加锁时,设置一个超时时间为1ms,如果这里时间内没有获得锁,就反复获得,直接获得到对文件操作权为止,当然。如果超时限制已到,就必需马上退出,让出锁让其它进程来进行操作。
        do {
            $canWrite = flock($fp, LOCK_EX);
            if (!$canWrite) {
                usleep(round(rand(0, 100) * 1000));
            }
        } while ((!$canWrite) && ((microtime() - $startTime) < 1000));
        if ($canWrite) {
            fwrite($fp, $content);
        }
        fclose($fp);
    }
}

 

示例文件:[下载]

版权声明:本文采用署名-非商业性使用-相同方式共享(CC BY-NC-SA 3.0 CN)国际许可协议进行许可,转载请注明作者及出处。
本文标题:php高效遍历文件夹、高效读取文件
本文链接:http://www.cnblogs.com/sochishun/p/7375328.html
本文作者:SoChishun (邮箱:14507247#qq.com | 博客:http://www.cnblogs.com/sochishun/)
发表日期:2017年8月16日

posted on 2017-08-16 19:36  sochishun  阅读(2098)  评论(0编辑  收藏  举报