【问题标题】:Can create zip archive in PHP, but cannot download it correctly可以在 PHP 中创建 zip 存档,但无法正确下载
【发布时间】:2021-01-27 08:17:58
【问题描述】:

我正在开发一个基于 CodeIgniter 的网络应用程序,并且我正在实现在管理员中下载车辆图像的 zip 文件以传递给第三方的功能。

我能够毫无问题地创建 zip 存档 - 如果我注释掉后面的部分,我可以看到 zip 文件已创建并包含正确的文件,并且可以正常提取。

但是,在文件创建后允许用户下载文件被证明是棘手的。我已经实现的允许下载具有正确名称的文件,但在解压缩时会出错,并显示以下消息:

End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.

知道我哪里出错了吗?我在网上查了多个教程,找不到问题所在。

public function downloadvehicleimages()
{
    // If logged in...
    if ($this->adminuser)
    {
        // Get data
        $usedcars = $this->adminmodel->getUsedCarsWithLimit();

        // Get the registrations for these vehicles
        $registrations = array();
        foreach($usedcars->result_array() as $usedcar)
        {
            array_push($registrations, $usedcar['Registration']);
        }

        // Get all the images for these registrations
        $images = array();
        $original_dir = getcwd();
        chdir('../used-cars/static/images/custom/');
        foreach($registrations as $registration)
        {
            $vehicle_images = glob(strtoupper($registration) . '_*.jpg');
            sort($vehicle_images);
            foreach($vehicle_images as $vehicle_image)
            {
                array_push($images, $vehicle_image);
            }
        }

        // Zip them up
        $zipname = 'images.zip';
        $zip = new ZipArchive;
        $zip->open($zipname, ZipArchive::CREATE);
        foreach($images as $image)
        {
            $zip->addFile($image);
        }
        $zip->close();

        // Let user download the file
        $this->output->set_header('Content-Type: application/zip');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header('Expires: 0'); 
        $this->output->set_header("Content-Disposition: attachment;filename='$zipname'");
        $this->output->set_header('Content:Length: ' . filesize($zipname));
        unlink($zipname);
        chdir($original_dir);
    }
}

【问题讨论】:

  • 我会接受有人刚刚提出的答案,但它被删除了

标签: php codeigniter ziparchive


【解决方案1】:

您需要使用 fopen() 来读取 zipfile 的内容,并使用 readfile() 将文件内容传递给访问者。此外,将完整的系统目录 uri 提供到文件位置:

        $filename = 'images.zip';
        $path = /var/www/yourdomain.com/images.zip';
        if(!file_exists($path))
        {
            die('Error: file not found');
        }
        else {
            $handle = fopen($path, "r");
            header('Content-Description: File Transfer');
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename='.$filename);
            header('Content-Transfer-Encoding: binary');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Expires: 0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($path));

            flush();
            readfile($path);
            fclose($handle);        

【讨论】:

  • 感谢您的努力,但是其他人加入了一个更短的解决方案(使用 CodeIgniter 中的 force_download() 辅助方法),但由于某种原因他们删除了它。
  • 对于那些不使用 CodeIgniter(我自己)的人来说,fopen/flush/readfile/fclose 解决方案就像一个魅力:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多