复制多级文件夹的递归功能不起作用 [英] Recursive function for copy of multilevel folder is not working
问题描述
复制
多层文件夹的递归函数无效。
Recursive function for copy of multilevel folder is not working.
一个代码复制所有的mulitilevel文件夹到新的文件夹。
I have a code to copy all the mulitilevel folder to new folder.
但在中间我觉得有正确的路径识别的问题,请参见下面的代码..
But in between I feel there is problem of proper path recognition, see the code below..
<?php
$source = '/var/www/html/pranav_test';
$destination = '/var/www/html/parth';
copy_recursive_dirs($source, $destination);
function copy_recursive_dirs($dirsource, $dirdest)
{
// recursive function to copy
// all subdirectories and contents:
if(is_dir($dirsource))
{
$dir_handle=opendir($dirsource);
}
if(!is_dir($dirdest))
{
mkdir($dirdest, 0777);
}
while($file=readdir($dir_handle))
{/*echo "<pre>";
print_r($file);*/
if($file!="." && $file!="..")
{
if(!is_dir($dirsource.DIRECTORY_SEPARATOR.$file))
{
copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file);
}
else{
copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest);
}
}
}
closedir($dir_handle);
return true;
}
?>
从上面的代码if循环有一个复制功能根据要求,这里是不适当的,我已经尝试了basename函数以及..但它没有得到预期的结果..下面是if循环我谈论与描述输出...
from the above code the if loop has a copy function as per requirement, but the path applied for destination here is not proper, I have tried with basename function as well.. but it didnt got the expected result.. below is the if loop i am talking about with comment describing the output...
if(!is_dir($dirsource.DIRECTORY_SEPARATOR.$file))
{
$basefile = basename($dirsource.DIRECTORY_SEPARATOR.$file);//it gives the file name
echo "Pranav<br>".$dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file;//it outputs for example "/var/www/html/parth//var/www/html/pranav_test/media/system/js/caption.js" which is not correct..
copy ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$dirsource.DIRECTORY_SEPARATOR.$file);
}
如上所示我无法得到文件和文件夹复制到预期的路径。 。请指导我在函数中放置正确的路径....
as shown above the i cannot get the files and folders copied to expected path.. please guide me to place proper path in the function....
推荐答案
我在代码中看到一些奇怪的事情dir和文件的目标路径,尝试使用此代码(未测试):
I see some strange things in the code about the destination path of dir and files, try with this code (not tested):
<?php
$source = '/var/www/html/pranav_test';
$destination = '/var/www/html/parth';
copy_recursive_dirs($source, $destination);
function copy_recursive_dirs($dirsource, $dirdest)
{
// recursive function to copy
// all subdirectories and contents:
if(is_dir($dirsource))
{
$dir_handle=opendir($dirsource);
}
if(!is_dir($dirdest))
{
mkdir($dirdest, 0777);
}
while($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 ($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
}
else{
//Copy the dir at the same lavel in the destination folder
copy_recursive_dirs($dirsource.DIRECTORY_SEPARATOR.$file, $dirdest.DIRECTORY_SEPARATOR.$file);
}
}
}
closedir($dir_handle);
return true;
}
?>
这篇关于复制多级文件夹的递归功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!