bobcooper Posted January 26, 2015 Share Posted January 26, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/294244-ziparchive-problems/ Share on other sites More sharing options...
CroNiX Posted January 26, 2015 Share Posted January 26, 2015 Try using a full system filepath (not url) to the image. '/home/user/www/images/imagename.jpg' or whatever the full path is. Quote Link to comment https://forums.phpfreaks.com/topic/294244-ziparchive-problems/#findComment-1504261 Share on other sites More sharing options...
bobcooper Posted January 27, 2015 Author Share Posted January 27, 2015 Hi, Thanks for your reply, I've tried adding $_SERVER["DOCUMENT_ROOT"] to the $imageDir but it still does not work and it also does not echo the images either. Bob Quote Link to comment https://forums.phpfreaks.com/topic/294244-ziparchive-problems/#findComment-1504326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.