与源文件夹相比,从目标文件夹中删除不需要的文件和文件夹
我正在使用 PHP,我需要编写如下所示的脚本:
我必须比较两个文件夹结构 并参考源文件夹 I 想要删除所有文件/文件夹 存在于其他目标文件夹中 参考源中不存在的内容 文件夹,我该怎么做?
编辑:
$original = scan_dir_recursive('/var/www/html/copy2');
$mirror = scan_dir_recursive('/var/www/html/copy1');
function scan_dir_recursive($dir) {
$all_paths = array();
$new_paths = scandir($dir);
foreach ($new_paths as $path) {
if ($path == '.' || $path == '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $path;
if (is_dir($path)) {
$all_paths = array_merge($all_paths, scan_dir_recursive($path));
} else {
$all_paths[] = $path;
}
}
return $all_paths;
}
foreach($mirror as $mirr)
{
if($mirr != '.' && $mirr != '..')
{
if(!in_array($mirr, $original))
{
unlink($mirr);
// delete the file
}
}
}
上面的代码显示了我做了什么.. 这里我的 copy1 文件夹包含比 copy2 文件夹额外的文件,因此我需要删除这些额外的文件。
编辑: 下面给出的输出是原始镜像的数组以及两者的差异..
Original Array
(
[0] => /var/www/html/copy2/Copy (5) of New Text Document.txt
[1] => /var/www/html/copy2/Copy of New Text Document.txt
)
Mirror Array
(
[0] => /var/www/html/copy1/Copy (2) of New Text Document.txt
[1] => /var/www/html/copy1/Copy (3) of New Text Document.txt
[2] => /var/www/html/copy1/Copy (5) of New Text Document.txt
)
Difference Array
(
[0] => /var/www/html/copy1/Copy (2) of New Text Document.txt
[1] => /var/www/html/copy1/Copy (3) of New Text Document.txt
[2] => /var/www/html/copy1/Copy (5) of New Text Document.txt
)
当我迭代循环以删除差异数组时,必须根据显示的输出删除所有文件..我如何纠正这个问题..删除的循环是下面给出。
$dirs_to_delete = array();
foreach ($diff_path as $path) {
if (is_dir($path)) {
$dirs_to_delete[] = $path;
} else {
unlink($path);
}
}
while ($dir = array_pop($dirs_to_delete)) {
rmdir($dir);
}
I am using PHP and I need to script something like below:
I have to compare two folder structure
and with reference of source folder I
want to delete all the files/folders
present in other destination folder
which do not exist in reference source
folder, how could i do this?EDITED:
$original = scan_dir_recursive('/var/www/html/copy2');
$mirror = scan_dir_recursive('/var/www/html/copy1');
function scan_dir_recursive($dir) {
$all_paths = array();
$new_paths = scandir($dir);
foreach ($new_paths as $path) {
if ($path == '.' || $path == '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $path;
if (is_dir($path)) {
$all_paths = array_merge($all_paths, scan_dir_recursive($path));
} else {
$all_paths[] = $path;
}
}
return $all_paths;
}
foreach($mirror as $mirr)
{
if($mirr != '.' && $mirr != '..')
{
if(!in_array($mirr, $original))
{
unlink($mirr);
// delete the file
}
}
}
The above code shows what i did..
Here My copy1 folder contains extra files than copy2 folders hence i need these extra files to be deleted.
EDITED:
Below given output is are arrays of original Mirror and of difference of both..
Original Array
(
[0] => /var/www/html/copy2/Copy (5) of New Text Document.txt
[1] => /var/www/html/copy2/Copy of New Text Document.txt
)
Mirror Array
(
[0] => /var/www/html/copy1/Copy (2) of New Text Document.txt
[1] => /var/www/html/copy1/Copy (3) of New Text Document.txt
[2] => /var/www/html/copy1/Copy (5) of New Text Document.txt
)
Difference Array
(
[0] => /var/www/html/copy1/Copy (2) of New Text Document.txt
[1] => /var/www/html/copy1/Copy (3) of New Text Document.txt
[2] => /var/www/html/copy1/Copy (5) of New Text Document.txt
)
when i iterate a loop to delete on difference array all files has to be deleted as per displayed output.. how can i rectify this.. the loop for deletion is given below.
$dirs_to_delete = array();
foreach ($diff_path as $path) {
if (is_dir($path)) {
$dirs_to_delete[] = $path;
} else {
unlink($path);
}
}
while ($dir = array_pop($dirs_to_delete)) {
rmdir($dir);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您需要两个目录的递归列表。像这样的简单函数就可以工作:
然后您可以使用
array_diff
计算它们的差异。迭代该数组,您将能够开始删除文件。目录有点棘手,因为它们首先必须是空的。
我已经测试过了,现在应该运行良好。当然,不要相信我的话。在删除真实数据之前,请确保设置您自己的安全测试。
First you need a recursive listing of both directories. A simple function like this will work:
Then you can compute their difference with
array_diff
.Iterating over this array, you will be able to start deleting files. Directories are a bit trickier because they must be empty first.
I've tested things and it should be working well now. Of course, don't take my word for it. Make sure to setup your own safe test before deleting real data.
对于递归目录,请使用:
您可以执行其他操作,例如 $file->isDot() 或 $file->isFile()。有关 SPLFileInfo 的更多文件命令,请访问 http://www.php.net/manual /en/class.splfileinfo.php
For recursive directories please use:
You can do other things like $file->isDot(), or $file->isFile(). For more file commands with SPLFileInfo visit http://www.php.net/manual/en/class.splfileinfo.php
感谢大家为我的工作付出的宝贵时间,特别感谢 erisco 对我的问题的奉献,下面的代码是完成我应该做的任务的完美代码,对 erisco 最后编辑的回复做了一些修改。 。
Thanks all for the precious time given to my work, Special Thanks to erisco for his dedication for my problem, Below Code is the perfect code to acomplish the task I was supposed to do, with a little change in the erisco's last edited reply...
首先对原始文件夹执行 scandir(),
然后对镜像文件夹执行 scandir。
开始遍历镜像文件夹数组并检查该文件是否存在于原始文件夹的 scandir() 中。像这样的东西
这应该可以解决你的问题。您需要相应地修改上面的代码(在上面的代码中包含一些递归来检查您要删除的文件夹是否为空,如果不为空,那么您首先需要删除所有文件/文件夹,然后删除父文件夹)。
First do a scandir() of the original folder,
then do a scandir on mirror folder.
start traversing the mirror folder array and check if that file is present in the scandir() of original folder. something like this
this should solve your problem. you will need to modify the above code accordingly (include some recursion in the above code to check if the folder that you are trying to delete is empty or not, if it is not empty then you will first need to delete all the file/folders in it and then delete the parent folder).