Jump to:

23453 Posts in 18902 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Dynamically Zipping Assets

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

Page: 1
Go to End
Author Topic: 1021 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Dynamically Zipping Assets Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Dynamically Zipping Assets Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Dynamically Zipping Assets Link to this post

    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.

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: Dynamically Zipping Assets Link to this post

    @zenmonkey Were you able to get this working?

    1021 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.