Monday, 9 June 2014

Copy file from one folder to another in php

Below code is delete the file from one folder and copy in destination folder.

<?php

  $dir_path = getcwd();
   $dir = $dir_path."/folder1/";
   $dirNew =$dir_path."/folder2/";
    if (is_dir($dir)) {

        if ($dh = opendir($dir)) {

            while (($file = readdir($dh)) !== false) {

            if ($file==".") continue;
            if ($file=="..")continue;
chmod("$dir", 0777);
chmod("$dirNew", 0777);
            if (copy("$dir/$file","$dirNew/$file"))
                {
                 if(unlink("$dir/$file")){
                     echo "Delete Sucessfully";
                 }
                echo "Files Copyed Successfully";
                 
                }
                else {echo "File Not Copy";}
            }
            closedir($dh);
        }
    }

?>

No comments: