0

I want to delete the directory from server after the download begins in php.

Here is the sample code for php to make dir and download it.

$dir = 'tested';

$archive = time().'download.zip';

$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);

$files = scandir($dir);
unset($files[0], $files[1]);
foreach ($files as $file) {
    $zip->addFile($dir.'/'.$file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);

unlink($archive);

I want to delete the directory once i am able to check the download status.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • Are you asking how to [remove a directory with files in it](http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it), or how to check the download status? Your question seems like a duplicate of the question I linked to. – alexis Mar 22 '17 at 11:37
  • Use this if we understand your question correctly, `if (!is_dir('tested')) {rmdir('tested');}` – CodeMonkey Mar 22 '17 at 11:39
  • @alexis i want to delete directory after download it – Jass Grewal Mar 22 '17 at 11:47
  • @Jass, Did you look at the link I posted? – alexis Mar 22 '17 at 12:09
  • @Inuka, you want to `rmdir` if it is _not_ a directory? That makes absolutely no sense. Anyway the OP knows it's a directory. – alexis Mar 22 '17 at 12:09
  • @alexis sorry my bad. the code should be like this: `if (is_dir('tested')) {rmdir('tested');}` therefore the code will check the directory exists or not before deleting it. – CodeMonkey Mar 22 '17 at 12:11

0 Answers0