创建后移动或删除并强制下载存档

时间:2016-02-11 20:44:32

标签: php download directory archive

简单地说,我希望我的用户能够下载他们的网站文件,因此我创建了一个“下载网站”按钮,该按钮使用此脚本将所有文件/文件夹添加到其变量{{}目录中{1}}并归档这些文件/文件夹。

$direc

令我惊讶的是,此代码有效。虽然,但有一些小问题:

  1. 它不会自动强制下载该存档。
  2. 它将存档添加到我的主目录中,而不是将其移动到我选择的单独目录(例如 <? ///////// DOWNLOAD ENTIRE WEBSITE: if(isset($_POST['download_site'])){ // define some basics $rootPath = '../useraccounts/'.$direc.''; $archiveName = ''.$direc.'.zip'; // initialize the ZIP archive $zip = new ZipArchive; $zip->open($archiveName, ZipArchive::CREATE); // create recursive directory iterator $files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY); // let's iterate foreach ($files as $name => $file) { $filePath = $file->getRealPath(); $zip->addFile($filePath); } // close the zip file if (!$zip->close()) { echo '<p>There was a problem writing the ZIP archive.</p>'; } else { echo '<p>Successfully created the ZIP Archive!</p>'; } } ?> )或在完成下载时将其删除。
  3. 这些问题是否可以解决,或者如果没有,是否有更好的方法可以做到这一点,因此我的主目录不会被持续下载所填补?我想,一旦创建了Archive多次,它将导致问题,因为它使用目录名称。

1 个答案:

答案 0 :(得分:0)

通过使用几种不同的组合来解决这个问题:

:api

这似乎解决了所有问题。