Question

I use this code to move files from one folder to another.

$src = path_a;
$dest = path_b;
// get files
$files = scandir($src);
// movin files
foreach ($files as $file){
    if (in_array($file, array(".",".."))) continue;
    if (copy($src.$file, $dest.$file)){
        $delete[] = $src.$file;
    }
}
// del files
foreach ($delete as $file) {
    unlink($file);
}

It works, but i have one subfolder with some files in source foulder ("path_a"). How i can move this subfolder with files to "path_b" and delete path_a directory?

Was it helpful?

Solution

The easiest way:

foreach ($files as $file){
    if (in_array($file, array(".",".."))) continue;
    rename($src.$file, $dest.file);
}

For more details, have a look to rename.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top