dongling5411 2013-05-09 01:13
浏览 36
已采纳

php zip文件没有得到所有图像

I am trying to create a zip file of all the images that are stored in a database. This is done when the user clicks on a download button. Right now, you click on the download button, the zip file is created and the window pops up asking you if you want to open the file or save it to your computer. If you open the file, it shows you all the file names (like it's supposed to). The problem is, when you try to view the images, they are all the SAME image, just with different names.

Here's my code:

    /* creates a compressed zip file */
function create_zip($files = array(), $destination = '', $overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $path = 'admin/ReviewFiles/';
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            $fullFile = $path . $file;
            if(file_exists($fullFile)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file);
        }

        //close the zip -- done!
        $zip->close();

        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}

And here's where the function is called:

    $i = 0;
    $reviewFiles = array();
    while ($row = mysql_fetch_object($result))
    {
        $reviewFiles[$i] = $row->fileName;
        $i++;
    }
    $zipCreated = create_zip($reviewFiles, 'reviewFiles.zip', true);
    $file = 'reviewFiles.zip';
if ($zipCreated) {
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename=reviewFiles.zip');
    header('Expires: 0');
    header('Pragma: no-cache');
    readfile("reviewFiles.zip");
    exit;
}

I have tried everything I can think of to figure out why it's not actually downloading each image. So, I guess clearly put, my question is: How can I fix the code to make it actually zip each file, not just one image with the same image with different names?

Thanks!

  • 写回答

1条回答 默认 最新

  • dsf323233323332 2013-05-09 01:17
    关注

    This:

    if(file_exists($fullFile)) {
        $valid_files[] = $file;
    }
    

    should be:

    if(file_exists($fullFile)) {
        $valid_files[] = $fullFile;
    }
    

    Otherwise the files could not be found. If you have changed this, you may also wish to set a local name:

    foreach($valid_files as $file) {
        $zip->addFile($file, basename($file));
    }
    

    I'm using basename() to prevent zip from creating sub directories in the archive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算