我跟着this SO thread递归删除目录(参见下面的代码)。问题是我压缩目录的内容并下载了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);
答案 0 :(得分:1)
我遇到了同样的问题并找到了解决方案。
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');
这对我有用。
答案 1 :(得分:0)
要递归删除目录,可以使用此代码。
注意: $ var可以是文件或目录。如果是目录,则删除所有内容和目录
资料来源:http://php.net/manual/en/function.rmdir.php,请查看gurch dot com上的jurchiks101评论。
if(file_exists($var))
{
if (PHP_OS === 'Windows')
{
exec("rd /s /q {$var}");
}
else
{
exec("rm -rf {$var}");
}
}