Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

codeigniter - Can create zip archive in PHP, but cannot download it correctly

I'm working on a CodeIgniter-based web app, and I'm implementing the functionality to download a zip file of the vehicle images in the admin for passing to third parties.

I'm able to create the zip archive without problem - if I comment out the later parts, I can see that the zip file gets created and contains the correct files, and it can be extracted as normal.

However, allowing the user to download the file once it's created is proving tricky. What I've implemented allows a file with the correct name to be downloaded, but it errors when unzipped with the following message:

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.

Any idea where I've gone wrong? I checked multiple tutorials on the web and couldn't find where the issue was.

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use fopen() to read the content of the zipfile, and readfile() to pass the file content to the visitor. And also, provide the full system directory uri to the files location:

        $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);        

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...