vknt Posted March 23, 2012 Posted March 23, 2012 Hey everyone, Processwire is such f** great tool to structure content that I use it in every possible situation. So now I'm using it to generate a datamodel for an iPad application (probably a 1000+ line datamodel) It's a kind of keynote like application. So I setup a tree with all al the slides (images) I need and various other pages to define variables for those slides. To generate the text datamodel, I just use php. No problem, but I'm stuck with the assets: What I want to be able to do is generate various sizes of the slides and then package those as a zip and force download that to the browser. Any guides on how to accomplish this? Thanks in advance! Valentijn
WillyC Posted March 23, 2012 Posted March 23, 2012 $zip = $page->images->path . 'dosfile.zip'; $fileos = array(); foreach($page->images as $image) $fileos[] = $image->filename; $fileos = implode(' ', $fileos); exec("zip $dosfile $fileos"); echo "<p><a href='$dosfile'>clicka.mato dawnlode</a></p>"; 1
Adam Kiss Posted March 24, 2012 Posted March 24, 2012 Just a note: WillyC's post of course assumes you have exec() rights to your server.
vknt Posted March 24, 2012 Author Posted March 24, 2012 Hi thanks for the responses. I tried the exec approach, but that didn't really work for me so I ended up using the ZipArchive class. http://www.php.net/m....ziparchive.php This is the basic approach of my code. <?php // ************************************************ // Zip files // ************************************************ $zip_filename = "assets.zip"; $zip_path = $config->paths->root."site/assets/files/".$page->id."/".$zip_filename; $zip_url = $config->urls->root."site/assets/files/".$page->id."/".$zip_filename; $zip = new ZipArchive; $res = $zip->open($zip_path, ZipArchive::CREATE); $log_errors = ""; if ($res === TRUE) { foreach ($pages->get("/slides/")->children() as $slide) { if ($slide->screen_image_nl->filename) { // add image - size iPad $slide_nl = $slide->screen_image_nl->size(1024,768)->filename; $zip->addFile($slide_nl, basename($slide_nl)); // add image - thumbnail $slide_thumb_nl = $slide->screen_image_nl->size(128,96)->filename; $zip->addFile($slide_thumb_nl, basename($slide_thumb_nl)); } else { $log_errors .= "slide (id:".$slide->id.") - ".$slide->title." <strong>does not exist!</strong><br/>"; } } $zip->close(); $zip_html = "<a href='$zip_url'>Download $zip_filename</a>"; } else { $zip_html = 'Oops something went wrong. I cannot give you the .zip file.'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Download assets</title> </style> </head> <body> <h4>Download</h4> <p><?php echo $zip_html; ?></p> <h5>ERRORS:</h5> <p><?php echo $log_errors == "" ? "No errors" : $log_errors; ?></p> </body> </html> 1
Pete Posted March 24, 2012 Posted March 24, 2012 I just remembered I've used the ZipArchive class recently too. I've got an inteanet system where zip files are uploaded and I have to check for a readme.csv file and it's perfect for that as there's very little overhead (as in it doesn't unzip the whole file but can search for a single file and pull it out very efficiently). That's not what we're doing here obviously, but it is cool!
Soma Posted March 24, 2012 Posted March 24, 2012 I thought there's some php module by default for zipzap? http://php.net/manual/en/book.zip.php though never really used it. http://www.php.net/manual/en/refs.compression.php
vknt Posted March 24, 2012 Author Posted March 24, 2012 I thought there's some php module by default for zipzap? http://php.net/manual/en/book.zip.php though never really used it. http://www.php.net/m...compression.php That's the one I used
bora Posted October 8, 2015 Posted October 8, 2015 I just coded this functionality with updated methods of PW. I use it to give all the images of the news page as a package for press release purposes. This is triggered by a button on the page like below, so the link above does not change, page is stable but download starts. <button onclick="window.location.href='<?= $page->httpUrl; ?>?download=images'"> if ($input->get->download == 'images') { if ($page->images->count() > 0) { //collect images $imagesArray = []; foreach ($page->images as $img) { $imagesArray[] = $img->filename; } //zip images //TODO: in a function $zipPath = $config->paths->files . $page->id . DIRECTORY_SEPARATOR . $page->name . '.zip'; wireZipFile($zipPath, $imagesArray, [ 'overwrite' => true ]); $options = array( // boolean: halt program execution after file send 'exit' => true, // boolean|null: whether file should force download (null=let content-type header decide) 'forceDownload' => true, // string: filename you want the download to show on the user's computer, or blank to use existing. 'downloadFilename' => $page->name . '.zip' ); wireSendFile($zipPath, $options); exit; } } 1
diogo Posted October 8, 2015 Posted October 8, 2015 <button onclick="window.location.href='<?= $page->httpUrl; ?>?download=images'"> Just curious, why aren't you using an anchor instead of a button with onclick?
bora Posted October 8, 2015 Posted October 8, 2015 Just curious, why aren't you using an anchor instead of a button with onclick? To prevent open in new window or save link as by user.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now