doumao1887 2015-03-25 13:01
浏览 131

调试zip存档和下载

I'd like a bit of help debugging this code. I'm trying to zip some files, which I provide as paths to Ziparchive. I think the zip file is empty, possibly. When I test this on a staging site I get a 'decompression failed' error. When I test this locally I get the following error in php logs

PHP Warning: readfile(images.zip): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/8018_Whites/wordpress/downloadzip.php on line 29.

Here is the code:

<?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();
}
?>

Do I need to add ob_start/ob_flush to this code, and should I put the ob_flush after readfile?

  • 写回答

2条回答 默认 最新

  • doulu0266 2018-08-06 09:14
    关注

    All files and Zip folder to give absolute path. so zip is properly created.

    <?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();
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入