I need to copy only selected files and folders in PHP
Posted
by OM The Eternity
on Stack Overflow
See other posts from Stack Overflow
or by OM The Eternity
Published on 2010-05-20T06:29:10Z
Indexed on
2010/05/20
6:30 UTC
Read the original article
Hit count: 217
I am using the following code, in which initially i am taking the difference of two folder structure and then the out put needs to be copied to other folder. here is the code below..
$source = '/var/www/html/copy1';
$mirror = '/var/www/html/copy2';
function scan_dir_recursive($dir, $rel = null) {
$all_paths = array();
$new_paths = scandir($dir);
foreach ($new_paths as $path) {
if ($path == '.' || $path == '..') {
continue;
}
if ($rel === null) {
$path_with_rel = $path;
} else {
$path_with_rel = $rel . DIRECTORY_SEPARATOR . $path;
}
$full_path = $dir . DIRECTORY_SEPARATOR . $path;
$all_paths[] = $path_with_rel;
if (is_dir($full_path)) {
$all_paths = array_merge(
$all_paths, scan_dir_recursive($full_path, $path_with_rel)
);
}
}
return $all_paths;
}
$diff_paths = array_diff(
scan_dir_recursive($mirror),
scan_dir_recursive($source)
);
/*$diff_path = array_diff($mirror,$original);*/
echo "<pre>Difference ";print_r($diff_paths);
foreach($diff_paths as $path)
{
echo $source1 = "var/www/html/copy2/".$path;
echo "<br>";
$des = "var/www/html/copy1/".$path;
copy_recursive_dirs($source1, $des);
}
function copy_recursive_dirs($dirsource, $dirdest)
{
$dir_handle=opendir($dirsource);
mkdir($dirdest,0777);
while(false!==($file=readdir($dir_handle)))
{/*echo "<pre>";
print_r($file);*/
if($file!="." && $file!="..")
{
if(is_dir($dirsource.DIRECTORY_SEPARATOR.$file))
{
//Copy the file at the same level in the destination folder
copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
}
else{
//Copy the dir at the same lavel in the destination folder
copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
}
}
}
closedir($dir_handle);
return true;
}
Whenever I execute the script I get the difference output but do not get the other copy on second folder as per code... Pls help me in rectifying...
© Stack Overflow or respective owner