dt4233
dt4233
2017-04-19 12:24

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

已采纳

folder structure

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;

}

header('Content-type: application/json');

echo json_encode($response);

The problem Is i get All File Expect Main Movie File (700Mb) here is
Working Example

Also Need Sorting show 700mb First then show other file

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douminfu8033 douminfu8033 4年前
    <?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));
    

    this should work for you. customize the output accordingly.

    点赞 评论 复制链接分享
  • duanmu2015 duanmu2015 4年前
    1. Why not using scandir()?
    2. You can try using glob() instead if scandir

      foreach( glob( $log_directory . '/.' ) as $file ) { // Function goes here.. }

    点赞 评论 复制链接分享