简体   繁体   中英

get All File from folder in 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

  1. Why not using scandir() ?
  2. You can try using glob() instead if scandir

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

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM