1

I followed this SO thread to recursively delete a directory (see code below). 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.

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); 
Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299
  • It seems that PHP and your server directories are tied up together and after the zip it is saying hey I'm not done and these dir/files are not free... (not sure the culprit) BUT what happens when you do a copy say to a scratch directory, zip the folders and files, THEN delete the original directory? Just curious. – Frank Tudor Apr 14 '14 at 21:06
  • hi @FrankTudor thanks, I am making a new directory each time. – tim peterson Apr 14 '14 at 21:17
  • Just curious..are you on windows or linux? – Frank Tudor Apr 14 '14 at 21:19
  • have same issue on mac and on linux, i've tried both. – tim peterson Apr 14 '14 at 21:19
  • 1
    Ok have you tried the zip close() http://www.php.net/manual/en/function.zip-close.php it looks like it locks your files and directories until you explicitly call close. Apologies if you have tried this already. – Frank Tudor Apr 14 '14 at 21:27
  • hmm, good suggestion. Unfortunately, just adding that after `zip->download()` didn't have any effect. – tim peterson Apr 14 '14 at 21:51
  • 1
    You may have to refactor the code a bit... Check out how this SO coder set it up...More verbose but explicit... http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php – Frank Tudor Apr 14 '14 at 21:55
  • Hmm, the `new ZipArchive` method works but now I can't get rid of the whole path inside the zip file. Frustrating that this isn't easier. – tim peterson Apr 14 '14 at 22:32
  • Are your files (*.png) deleted? If no then so won't the parent dir. If your files do get deleted check PHP for warnings. It should throw something. Check permissions. – tlenss Apr 15 '14 at 19:16
  • The .png files aren't getting deleted. It's not a permission issue because the files can be deleted without any change in permissions if zipping is not involved. – tim peterson Apr 15 '14 at 21:50
  • @timpeterson: Did you find any solution to this. I facing the same issue right now. – Deepak Rai Dec 21 '14 at 07:42
  • @Deepak hmm, I can't remember. I think I may have given up, sorry. – tim peterson Dec 22 '14 at 08:24

2 Answers2

1

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.

Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
0

To recursively remove a directory, you can use this code.
NOTE: $var can be a file or directory. 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.

if(file_exists($var))
{
    if (PHP_OS === 'Windows')
    {
        exec("rd /s /q {$var}");
    }
    else
    {
        exec("rm -rf {$var}");
    }
}
kishorer747
  • 810
  • 1
  • 10
  • 24