Skip to content

Instantly share code, notes, and snippets.

@shmshd
Forked from baamenabar/move.php
Last active June 29, 2017 09:16
Show Gist options
  • Save shmshd/6e89628b76d9ea7449acfddc0f4c220a to your computer and use it in GitHub Desktop.
Save shmshd/6e89628b76d9ea7449acfddc0f4c220a to your computer and use it in GitHub Desktop.
PHP move all files and folders from one directory to a new directory
<?php
$dir = "."; //path to backup
$dirNew = "move"; //path to move the destination
if(!file_exist($dirNew){
mkdir($dirNew, 0777, true); //create a new directory if not exist
}
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo '<br>Archivo: '.$file;
//exclude unwanted
if ($file == basename(__FILE__))continue;
if ($file == ".") continue;
if ($file == "..")continue;
//if ($file=="index.php") continue; for example if you have index.php in the folder
if (rename($dir.'/'.$file,$dirNew.'/'.$file)){
echo " Files Copyed Successfully";
echo ": $dirNew/$file";
//if files you are moving are images you can print it from
//new folder to be sure they are there
}else{
echo "File Not Copy";}
}
closedir($dh);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment