Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从文件夹中获取所有文件_Php_Json_Directory - Fatal编程技术网

在php中从文件夹中获取所有文件

在php中从文件夹中获取所有文件,php,json,directory,Php,Json,Directory,文件夹结构 php代码 标题'Content-type:application/json' echo json_encode($response); 问题是除了主电影文件700Mb之外,我获得了所有文件 这是 还需要排序显示700mb,然后再显示其他文件 为什么不使用? 如果是scandir,您可以尝试使用 foreach glob$log_目录。“/”作为$file{ //函数在这里。。 } 这应该对你有用。相应地定制输出。主视频文件是否在返回的scandir$dir数组中丢失?是的,主视

文件夹结构

php代码

标题'Content-type:application/json'

echo json_encode($response);
问题是除了主电影文件700Mb之外,我获得了所有文件 这是

还需要排序显示700mb,然后再显示其他文件

为什么不使用? 如果是scandir,您可以尝试使用

foreach glob$log_目录。“/”作为$file{ //函数在这里。。 }


这应该对你有用。相应地定制输出。

主视频文件是否在返回的scandir$dir数组中丢失?是的,主视频是返回scandir$dir的任务,但它在服务器上退出。如果我删除任何png文件,则主视频文件以json_encode显示
$dir = "download/a176b8a9f5575a08eac602adfdc78f666e3695a2";

$response = scan($dir);

function scan($dir){
           if(file_exists($dir)){
            foreach(scandir($dir) as $f) {
              if(!$f || $f[0] == '.') {
                continue; // Ignore hidden files
              }

            if(is_dir($dir . '/' . $f)) {

                // The path is a folder

                $files = scan($dir . '/' . $f); // Recursively get the contents of the folder}

            else {

                // It is a file

                $files[] = array(
                    "name" => $f,
                    "type" => "file",
                    "path" => $dir . '/' . $f,
                    "size" => filesize($dir . '/' . $f) // Gets the size of this file

                );
            }
        }

    }
return $files;

}
echo json_encode($response);
<?php

class MyRecursiveFilterIterator extends RecursiveFilterIterator {
    public function accept() {
        if (substr($this->current()->getFilename(), 0, 1) !== '.') {
          return true;
        } else {
          return false;
        }
    }

}
$dir = 'dirname';
$dirItr = new RecursiveDirectoryIterator($dir);
$dirItr->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$filterItr = new MyRecursiveFilterIterator($dirItr);
$itr = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
$itr->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
foreach ($itr as $filePath => $fileInfo) {
  if ($fileInfo->isFile()) {
    $final[$fileInfo->getSize()][] = $fileInfo->getPathName(); //alternatively you can use $fileInfo->getFilename();
  }
}

krsort($final);
echo '<pre>';print_r(($final));