Hi there,
I'm trying to create a zip file from an array of images that are posted from a form using the following code:
<?php
$shortlist = $_POST['shortlist'];
$dir = $_POST['imageDir'];
$imageDir = $dir;
$imageArr = array();
$imageArr = preg_split('/\r\n|\r|\n/', $_POST['shortlist']);
array_shift($imageArr);
/*
foreach ($imageArr as &$value) {
$value = rtrim($value);
$value = $imageDir . "/" . $value;
}
*/
$imageTotal = count($imageArr);
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE) {
// Opening zip file to load files
echo "* Sorry ZIP creation failed at this time";
} else {
echo"zip created";
chmod($zip_name, 0755);
foreach($imageArr as $file) {
$hostedFile = $imageDir . '/' . $file;
echo '<img src="' . $hostedFile . '" />';
$zip->addFile($hostedFile,$file);
}
$close = $zip->close();
if ($close) {
echo 'File Closed';
} else {
echo 'fail!!';
}
echo 'Download these images as a <a href="'.$zip_name.'">ZIP file</a>.\n';
}
?>
The php file is in a separate folder e.g. www.website.com/php/zip-test.php to the images folder as I want to be able to re-use it for other folders.
I get as far as adding the files to the zip but when I try to close the zip it fails. The only thing I can think is that the $hostedFile is a relative link, e.g. /images/image1.jpg rather than http://www.website.com/images/image1.jpg so it's not finding the image to add to the zip
So I tried adding the full website path to the image but it still does not work. The image is displaying fine when I echo it as an image, it just doesn't add to the zip.
Any ideas?
Thanks,
Bob