【问题标题】:PHP File Manager Crashes When Reading Large Directory读取大型目录时 PHP 文件管理器崩溃
【发布时间】:2015-02-15 18:45:28
【问题描述】:

看起来 scandir() 在这里引起了问题,如果我可以设置深度限制,它可能会更好,但我不太确定。

我的网站有一个组织良好的大型图像目录 (25gb)。我将 tinyMCE 与文件管理器 Roxy Fileman 一起使用。当我尝试加载根文件夹“图像”时,它只会继续加载并最终崩溃。如果我对其进行硬编码以打开“图像/帖子”,它会很好地加载。我检查了源代码,看起来这个函数正在读取目录。

function listDirectory($path){
  $ret = @scandir($path);
  if($ret === false){
    $ret = array();
    $d = opendir($path);
    if($d){
      while(($f = readdir($d)) !== false){
        $ret[] = $f;
      }
      closedir($d);
    }
  }

  return $ret;
}

一个目录尤其包含大约 99% 的图像,我不需要使用文件管理器访问它,但我也无法移动该目录。 “图像/游戏”是原因。有没有办法让我忽略这个目录或只扫描到一定深度。

【问题讨论】:

    标签: php file-management


    【解决方案1】:

    如果有办法自己读取目录,您可以这样做并创建一个包含被忽略目录的列表。您说目录games 包含大约 99% 的图像,但您不需要访问它。这可能是被忽略目录的完美候选者。

    如果可能的话,我会写如下内容:

    function read_directory_files($path, array $ignored = []) {
    
        /*
         * Check if the provided path is readable and is a directory.
         */
        if(!is_readable($path) || is_file($path)) {
            throw new \LogicException('The provided path must be a readable directory.');
        }
    
        $files.      = [];
        $directories = scandir($path);
    
        /*
         * Remove current (.) and parent (..) directory references.
         */
        unset($directories[0], $directories[1]);
    
        /*
         * Check if the directory is empty after the current and parent
         * directory references has been removed.
         */
        if(empty($directories)) {
            return null;
        }
    
        foreach($directories as $directory) {
    
            if(!is_file($directory) && in_array($directory, $ignored)) {
                continue;
            }
    
            if(!is_file($directory)) {
    
                $key         = basename($directory); 
                $files[$key] = read_directory_files($directory);
    
            }else{
    
                $files[] = $directory;
    
            }
    
        }
    
        return $files;
    
    }
    

    此代码未经测试,因此可能会出现错误,但这应该使您能够选择要从中读取文件的目录。用法是:

    $ignored = [
       'games'
    ];
    
    $files = read_directory_files('path/to/images', $ignored);
    

    如果您不能使用自己的机制来读取目录,您可以尝试将现有代码替换为类似我编写的代码,但这样做需要您自担风险并记得备份您的代码。

    希望这对您有用,祝您编码愉快!

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      相关资源
      最近更新 更多