简体   繁体   中英

Move all files from sub directories into main directory

I have a main directory like :

/import/

and in /import/ i have lots of sub directories, containing audio files.

I would like to create a php script to move all the audio files from the sub directories into the main directory.

Thanks guys :)

$it = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator("/import/"));

$it->rewind();
while($it->valid()) {
    $full_path = $it->key();
    $relative_path = $it->getSubPath();
    if ($it->getDepth() > 0 && preg_match("/regex/", $relative_path)) [
        //move stuff
    }
    $it->next();
}

See RecursiveIteratorIterator and RecursiveDirectoryIterator . You could also encapsulate the iterator in a RegexIterator .

Have a look at opendir() , is_dir() , copy() and unlink() .

What you need to do is:

Open the /import directory and iterate through the listing.

For each entry, if it's a directory (and not . or ..), get the listing of that sub directory.

Then for each audio file in that sub directory, copy to /import/, then use unlink to delete.

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