1

この 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); 
4

2 に答える 2

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

これは私にとってはうまくいきました。

于 2014-12-21T08:14:01.563 に答える
0

ディレクトリを再帰的に削除するには、次のコードを使用できます。
注: $var は、ファイルまたはディレクトリにすることができます。ディレクトリの場合、すべてのコンテンツとディレクトリが削除されます。
ソース: http://php.net/manual/en/function.rmdir.php、gmail dot com の jurchiks101 によるコメントを参照してください。

if(file_exists($var))
{
    if (PHP_OS === 'Windows')
    {
        exec("rd /s /q {$var}");
    }
    else
    {
        exec("rm -rf {$var}");
    }
}
于 2014-11-16T10:46:13.767 に答える