从php

时间:2017-04-19 12:24:47

标签: php json directory

  

文件夹结构

Main Folder
     ->(1)SS (subFolder) contain 15 Png Images
     -> Main Movie File (700Mb)
     -> Sample.mkv (14Mb)
  

php code

$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;

}

标题('内容类型:application / json');

echo json_encode($response);
  

问题是我得到所有文件期待主电影文件(700Mb)     这是
       Working Example

     

还需要排序显示700mb首先显示其他文件

2 个答案:

答案 0 :(得分:1)

  1. 为什么不使用scandir()
  2. 如果是scandir

    ,您可以尝试使用glob()

    foreach(glob($ log_directory。' / ')为$ file){     //功能在这里.. }

答案 1 :(得分:0)

<?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));

这对你有用。相应地自定义输出。