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
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...');
}
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 ...
}
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
精彩评论