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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus