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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器