Tutorial :How to find larges file in a directory tree, in php?


I have a folder, that contains other folders, which may or may not contain other folders, where the files might reside. I want to scan the entire dir structure, and find files larger than 100MB, and move these files to the top level directory.

Whats a good way to accomplish this, in php?

For example:

/data/1/subfolder/id33/big_file.avi  /data/4/another_big_file.avi  /data/56/something/big_file2.avi  

I need to move these 3 files, into /data


class MyFilterIterator extends FilterIterator {      public function accept() {          return $this->getSize() > 100*1024*1024;      }  }    $it = new MyFilterIterator(new RecursiveIteratorIterator(      new RecursiveDirectoryIterator("/data")));    foreach($it as $file) {      //move ...  }  


Simply scan all subfolders with

$it = new RecursiveDirectoryIterator("/data");  foreach(new RecursiveIteratorIterator($it) as $file) {      //work with file (which is simplya a path here actually) here  }  

you'll add some condition there, such as

if (filesize($file) > 100e6) {      rename($file, '/data/filename...');  }  


If you use a DirectoryIterator from SPL to browse a directory and even recurse into it if you need. To take the next step, you can use a Filter to filter the results by filesize and then do what you need.

Here's a rough example: http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html#17

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »