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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Dynamically Zipping Assets


Go to End


5 Posts   3835 Views

Avatar
zenmonkey

Community Member, 545 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, 5523 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, 545 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?

Avatar
probablytony

Community Member, 4 Posts

24 November 2015 at 3:23pm

If anyone is interested, I got this working like so:

<?php
class ProductFile extends DataObject{
    
    private static $db = array (
        'Sort' => 'Int',
        'Title' => 'Varchar(255)'
    );

    private static $has_one = array (
        'ProductPage' => 'ProductPage',
        'ProductFileType' => 'ProductFileType',
        'DownloadFile' => 'File'
    );

    private static $summary_fields = array(
        'Title' => 'Title'
    );

    public function getCMSFields() {
        return new FieldList(
            TextField::create('Title', 'Title'),
            UploadField::create('DownloadFile', 'File')->setFolderName('Projects/Files'),
            DropdownField::create('ProductFileTypeID', 'File Type', ProductFileType::get()->where('SiteConfigID = 1')->map('ID', 'Title'))->setEmptyString('(Select one)', $this->ProductFileTypeID)
        );
    }
    
    public function onAfterWrite() {
        parent::onAfterWrite();
        $this->ZipFile();
    }
    
    /* creates a compressed zip file */
    
    public function GetAllDownloads() {
        $records = ProductFile::get()->filter('ProductPageID', $this->ID);
        $filePaths = array();

        if ($records) {
            foreach ($records as $record) {
                $filePaths[] = '../' . $record->DownloadFile()->FileName;
            }
            return $filePaths;
        } else {
            return false;
        }
    }
    
    public function ZipFile() {
        /* creates a compressed zip file */
        $files_to_zip = $this->GetAllDownloads();
        
        if ($files_to_zip) {
            $result = $this->CreateZip($files_to_zip, '../Downloadables/' . $this->ProductPage()->Title . '.zip', true);
            if ($result) {
                return $result;
            } else {
                //return "fail";
            }
        } else {
            return false;
        }
    }

    public function CreateZip($files = array(), $destination = '', $overwrite = 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;
                } else {
                    //return "File dosnt exist";
                }
            }
        }
        //if we have good files...
        if (count($files)) {
            //create the archive
            $zip = new ZipArchive();
            if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
                //return "Cannot create Zip";
            }
            //add the files
            foreach ($valid_files as $file) {
                $new_filename = substr($file,strrpos($file,'/') + 1);
                $zip->addFile($file, $new_filename);
            }

            //close the zip -- done!
            $zip->close();

            //check to make sure the file exists
            return file_exists($destination);
        } else {
            //return "overall fail";
        }
    }
    
}