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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…