创建一个ZIP文件下载,然后删除它


Create a ZIP file download and then delete it

我希望创建一个PHP脚本,它将从一个位置创建一个zip文件,这将是通过$_GET传递的变量。该位置将是一个文件夹,然后将被压缩,用户将提示下载该文件夹,下载后该文件夹将需要自动删除。

有人能帮忙吗?

谢谢

http://php.net/manual/en/ref.zip.php

你可以这样做:

  • 验证得到
  • 查找文件夹是否存在
  • 压缩文件夹
  • 读取新创建的zip文件并在末尾删除它,如下所示:

       header('Content-Description: File Transfer');
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename='.basename($file));
       header('Content-Transfer-Encoding: binary');
       header('Expires: 0');
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       header('Pragma: public');
       header('Content-Length: ' . filesize($file));
       ob_clean();
       flush();
       readfile($file); 
       @unlink($file);
    

代码取自这里:http://php.net/manual/en/function.readfile.php

从$_GET中读取位置并读取下面的所有文件并创建一个文件数组。然后创建一个压缩文件并下载。

下面的链接将帮助你http://www.tricksofit.com/2013/10/create-zip-file-php