Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move all files and folders in a folder to another?

Tags:

php

windows

I want to move all files and folders inside a folder to another folder. I found a code to copy all files inside a folder to another folder. move all files in a folder to another

// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "source/";
$destination = "destination/";
// Cycle through all source files
foreach ($files as $file) {
  if (in_array($file, array(".",".."))) continue;
  // If we copied this successfully, mark it for deletion
  if (copy($source.$file, $destination.$file)) {
    $delete[] = $source.$file;
  }
}
// Delete all successfully-copied files
foreach ($delete as $file) {
  unlink($file);
}

How do I change this to move all folders and files inside this folder to another folder.

like image 532
Sara Avatar asked Mar 23 '12 07:03

Sara


People also ask

Can you move multiple files all at once into a folder?

You can drag-and-drop, cut and paste, or use the "Move to Folder" command. Below are the steps on how you can move files in Windows. Choose the option that works best for you. You can also select multiple files and move multiple files at once using any of the steps below.

How do you move all files in a folder to another folder in Unix?

Using mv Command The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories. This will move all the files from /path/subfolder to /path/ except for hidden files and directories.


2 Answers

Use rename instead of copy.

Unlike the C function with the same name, rename can move a file from one file system to another (since PHP 4.3.3 on Unix and since PHP 5.3.1 on Windows).

like image 142
Joni Avatar answered Oct 26 '22 15:10

Joni


I think the answers are not complete for me, beacuse DIRECTORY_SEPARATOR are not defined on any answer (thans to Edgar Aivars for remember that!), but I want to write my solution for move (rename), copy and delete directory structures (based on this post info, the credits are for yours!).

defined('DS') ? NULL : define('DS',DIRECTORY_SEPARATOR);

function full_move($src, $dst){
    full_copy($src, $dst);
    full_remove($src);
}

function full_copy($src, $dst) {
    if (is_dir($src)) {
        @mkdir( $dst, 0777 ,TRUE);
        $files = scandir($src);
        foreach($files as $file){
            if ($file != "." && $file != ".."){
                full_copy("$src".DS."$file", "$dst".DS."$file");
            }
        }
    } else if (file_exists($src)){
        copy($src, $dst);
    }
}

function full_remove($dir) {
    if (is_dir($dir)) {
        $files = scandir($dir);
        foreach ($files as $file){
            if ($file != "." && $file != ".."){
                full_remove("$dir".DS."$file");
            }
        }
        rmdir($dir);
    }else if (file_exists($dir)){
        unlink($dir);
    }
}

I hope this hepls anyone! (lile me in the future :D )

EDIT: Spell corrections... :(

like image 43
chybeat Avatar answered Oct 26 '22 15:10

chybeat