[英]Recursively deleting a directory fails after zipping and downloading its contents
我按照此SO线程递归删除目录(请参见下面的代码)。 问题是我压缩目录的内容并下载了zip文件后,无法使这些命令执行其操作。
文件/文件夹权限似乎不是问题所在,因为正如我所说,如果不涉及文件夹压缩,代码就可以正常工作。
有人有想法么?
$this->zip->download($file_name); //a Codeigniter function, though think it could be any function that executes the zip file download.
$dir='uploads/folder1';
//the contents of folder1 are "foo1.png" and "foo2.png"
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
rmdir($dir);
我遇到了同样的问题并找到了解决方案。
protected function _deleteFolder($path = null) {
if (!$path || !file_exists($path)) {
return FALSE;
}
delete_files($path, true); // delete all files/folders
rmdir($path);
}
$folder_path = '/path/to/the/folder/to/be/zipped/downloaded';
$this->zip->read_dir($folder_path, FALSE);
$this->_deleteFolder($folder_path); // This will delete the folder
$this->zip->download('zipped-downloadable-file-name.zip');
这对我有用。
要递归删除目录,可以使用此代码。
注意: $ var可以是文件或目录。 如果是目录,则将删除所有内容和目录。
来源: http ://php.net/manual/en/function.rmdir.php,请看jurchiks101在gmail点com上的评论。
if(file_exists($var))
{
if (PHP_OS === 'Windows')
{
exec("rd /s /q {$var}");
}
else
{
exec("rm -rf {$var}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.