Php 调试zip存档和下载

Php 调试zip存档和下载,php,wordpress,ziparchive,Php,Wordpress,Ziparchive,我想要一些调试这段代码的帮助。我正在尝试压缩一些文件,这些文件是作为Ziparchive的路径提供的。我想zip文件可能是空的。当我在一个临时站点上测试这个时,我得到一个“解压缩失败”错误。当我在本地测试时,我在php日志中得到以下错误 PHP警告:readfile(images.zip):无法打开流:第29行的/Applications/MAMP/htdocs/8018_Whites/wordpress/downloadzip.PHP中没有这样的文件或目录 代码如下: <?php //

我想要一些调试这段代码的帮助。我正在尝试压缩一些文件,这些文件是作为Ziparchive的路径提供的。我想zip文件可能是空的。当我在一个临时站点上测试这个时,我得到一个“解压缩失败”错误。当我在本地测试时,我在php日志中得到以下错误

PHP警告:readfile(images.zip):无法打开流:第29行的/Applications/MAMP/htdocs/8018_Whites/wordpress/downloadzip.PHP中没有这样的文件或目录

代码如下:

<?php

//get image urls string
//$cat_string = "url1,url2,url3..."
if (isset($_GET['category-images'])) {
    $cat_string=$_GET['category-images'];
}
//change string to array
$cat_array=explode(",", $cat_string);
$files=$cat_array;

$zipname = 'images.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  if(!strstr($file,'.php')) $zip->addFile($file);
}
$zip->close();

if ($zipname) {
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
exit();
}
?>


是否需要将ob_start/ob_flush添加到此代码中,以及是否应在readfile之后放置ob_flush?

所有文件和Zip文件夹以提供绝对路径。因此,zip是正确创建的

<?php
$zip_root ='/var/www/gplhome/'
$file_root ='/var/www/gplhome/project/'

    //get image urls string
    //$cat_string = "url1,url2,url3..."
    if (isset($_GET['category-images'])) {
        $cat_string=$_GET['category-images'];
    }
    //change string to array
    $cat_array=explode(",", $cat_string);
    $files=$cat_array;

    $zipname = $zip_root.'images.zip';
    $zip = new ZipArchive;


    if ($zip->open($zipname, ZIPARCHIVE::CREATE) != TRUE) {
        die("Could not open archive");
    }

    foreach ($files as $file) {
      if(!strstr($file_root.$file,'.php')) $zip->addFile($file_root.$file);
    }
    $zip->close();

if ($zipname) {
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);
    exit();
}
?>

  • 检查$zip->close()的返回值,出错时返回false
  • 检查$zip->addFile()的返回值,出错时返回false
  • 确保仅使用文件名作为标题,使用全名作为readfile
  • 检查文件是否已创建。替换
if($zipname){

if(可读($zipname)){

  • 不要将
    ?>
    和php文件的结尾放在