Belgin Fish
Belgin Fish

Reputation: 19837

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 :)

Upvotes: 1

Views: 896

Answers (2)

Artefacto
Artefacto

Reputation: 97835

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

Upvotes: 3

Pete
Pete

Reputation: 1773

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.

Upvotes: 1

Related Questions