简体   繁体   English

在压缩并下载目录内容后,递归删除目录失败

[英]Recursively deleting a directory fails after zipping and downloading its contents

I followed this SO thread to recursively delete a directory (see code below). 我按照此SO线程递归删除目录(请参见下面的代码)。 The problem is I can't get these commands to do their things after I have zipped the directory's contents and downloaded the zip file. 问题是我压缩目录的内容并下载了zip文件后,无法使这些命令执行其操作。

File/folder permissions don't appear to be the issue because as I said the code works just fine if folder zipping isn't involved. 文件/文件夹权限似乎不是问题所在,因为正如我所说,如果不涉及文件夹压缩,代码就可以正常工作。

Anyone have any ideas? 有人有想法么?

$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); 

I faced the same issue and found the solution. 我遇到了同样的问题并找到了解决方案。

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');

This worked for me. 这对我有用。

To recursively remove a directory, you can use this code. 要递归删除目录,可以使用此代码。
NOTE: $var can be a file or directory. 注意: $ var可以是文件或目录。 If it is a directory, all contents and the directory are deleted. 如果是目录,则将删除所有内容和目录。
Source: http://php.net/manual/en/function.rmdir.php , look at the comment by jurchiks101 at gmail dot com. 来源: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM