Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Dynamically Zipping Assets


Reply


4 Posts   1070 Views

Avatar
zenmonkey

Community Member, 528 Posts

15 July 2010 at 7:54am

I'm trying to create a method to Dynamically zip assets to allow users to download all high res product folders in one zip file. I'm having trouble implementing the code. The zip file method works on its own outside of SS.

Right now I've just specified the paths to a few files as a test senerio, I keep getting a 500 error

function CreateZip($files = array(),$destination = '',$overwrite = false) {
      //if the zip file already exists and overwrite is false, return false
      if(file_exists($destination) && !$overwrite) { return false; }
      //vars
      $valid_files = array();
      //if files were passed in...
      if(is_array($files)) {
         //cycle through each file
         foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
               $valid_files[] = $file;
            }
         }
      }
      //if we have good files...
      if(count($valid_files)) {
         //create the archive
         $zip = new ZipArchive();
         if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
         }
         //add the files
         foreach($valid_files as $file) {
            $zip->addFile($file,$file);
         }
         //debug
         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
         
         //close the zip -- done!
         $zip->close();
         
         //check to make sure the file exists
         return file_exists($destination);
      }
      else
      {
         return false;
      }
   }

function ZipLink() {
      //$files_to_zip = GetAllImagePaths();
      $files_to_zip = array (
                      'assets/Products/Tower/Tower.jpg',
                      'assets/Products/Tower/Tower-Side.jpg',
                      'assets/Products/Classic/T0001-detail.jpg',
                      'assets/Products/Classic/T0001-box-2.jpg',
                      'assets/Products/Classic/T0002-detail.jpg',
                      'assets/Products/Classic/T0002-box-2.jpg',
                      'assets/Products/Classic/T0003-detail.jpg',
                      'assets/Products/Classic/T0003-box-2.jpg');

      
      if ($files_to_zip) {
         $result = CreateZip($files_to_zip, 'assets/my-archive.zip');
         if ($result) {
            return "success";
         } else {
            return "fail";
         }
      } else {
         return false;
      }
   }

Thanks for any Help

Avatar
Willr

Forum Moderator, 5513 Posts

15 July 2010 at 8:02pm

If its a 500 error I'm pretty sure you should see the error in your PHP error logs on your server (if you have display_errors in your php.ini) on. That will help to get down to the error.

Avatar
zenmonkey

Community Member, 528 Posts

16 July 2010 at 2:02am

Well I got access to my logs form my SysAdmin. And it was throwing a Unidentified function CreateZip so I moved the function into the ZipLink() function and that got rid of the error however its still failing. I added a few more else statements to return more errors on some of the ifs and I see where its falling apart.

Seems to be at:

if(file_exists($file)) {
$valid_files[] = $file;
}

So I changed the source array to include full paths with domain and Its still throwing an error at that point. For some reason it can't see the files. I even doubles checked the paths and set the permission to 777.

Avatar
moloko_man

Community Member, 72 Posts

4 January 2011 at 7:40am

@zenmonkey Were you able to get this working?