Page 2 of 5
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:04 am
by Celauran
Ah. You'd want the file path for that, not the web path. I'd specify that in the function that handles zip creation. If you haven't already, you may want to consider defining an image path constant that you can use throughout your app. I'd do that somewhere in the bootstrapping process.
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:05 am
by Celauran
How specifically you would do that would depend on how your app is structured, but look at things like __DIR__, dirname(), etc.
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:10 am
by simonmlewis
The array in that web site looks like it puts the filepath in there.
So I guess just some simple variable that stores the path, and then append that onto the query. But I don't know how I would do that. This Zip stuff is brand new to me.
Maybe something like this:
But how I tell it the path for it, when I am use to http://.... I don't know.
Nor do I know whether I Can put this part of the function into the area you and I have looked at, or not.
Let's say the path is httpdocs/images/productphotos ...??
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:15 am
by Celauran
simonmlewis wrote:But how I tell it the path for it, when I am use to http://.... I don't know.
You don't. Creating the zip file is all handled on the local file system, so you need to use file paths, not URIs.
Something like this, yes. Bear in mind that \ isn't a directory separator on Linux, which is likely where you'll be deploying. I believe Windows will understand if you use / for directory separators. If not, you can use the DIRECTORY_SEPARATOR constant.
Code: Select all
$image_path = __DIR__ . DIRECTORY_SEPARATOR . 'images';
define(IMAGE_PATH, $image_path);
or something along those lines.
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:17 am
by simonmlewis
This is where I am at (apart from the main bulk of the function on top of it).
Code: Select all
/* The directory you wish to scan for files or create an array in some other manner */
$target=__DIR__.'\my-archive.zip';
/* Scan the dir, or as mentioned, create an array of files some other way */
$files=glob( 'C:\Temp\temp_uploads\*.txt' );
/* directory where images are stored */
$image_path = __DIR__ . DIRECTORY_SEPARATOR . 'images/productphotos';
define(IMAGE_PATH, $image_path);
$query = "SELECT photo FROM products WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}
So how do I put that $image page into the array, so it knows where I am directing to look for the images?
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:27 am
by Celauran
sendfile isn't a built in function, so I don't know what it does. (I mean, I can infer from the name, but the inner workings are a black box). It could do everything required, or maybe not.
Looking at your example above, there remain a large number of hardcoded values. Also, if you're not defining IMAGE_PATH in your bootstrapping process, you're going to run into issues where you're trying to define something that's already defined. Move it to bootstrapping or skip the define() call altogether.
$files doesn't appear to be used, so you can remove that line entirely.
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:30 am
by Celauran
As for passing the full file path, you can prepend that to each item in your array easily enough with a simple foreach loop.
Code: Select all
$image_dir = dirname(__DIR__) . '/something/whatever/';
foreach ($files_to_zip as $index => $file) {
$files_to_zip[$index] = $image_dir . $file;
}
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:52 am
by simonmlewis
So where in this lot, do I put that prepend?
Code: Select all
/* The directory you wish to scan for files or create an array in some other manner */
$target=__DIR__.'\my-archive.zip';
/* directory where images are stored */
$image_path = __DIR__ . DIRECTORY_SEPARATOR . 'images/productphotos';
define(IMAGE_PATH, $image_path);
$query = "SELECT photo FROM products WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 11:55 am
by Celauran
Once you've finished creating your $files_to_zip array
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 12:38 pm
by simonmlewis
Sorry.....?
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 12:51 pm
by Celauran
Code: Select all
// Here we pull the photos from the database
$query = "SELECT photo FROM products WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
// Explode the 'photo' field into an array of individual photos
$files_to_zip = explode('|', $row['photo']);
// Add the photoprimary field to the array
$files_to_zip[] = $row['photoprimary'];
// We now have an array of all the photos we want to include in the zip
// File paths are as they appear in the database, not the full path to the files
// Store the full path to the image directory in a variable
$image_path = __DIR__ . '/images/something/path/here/';
// Now we go through each file in our array
foreach ($files_to_zip as $index => $file) {
// and inject that path to the beginning of each file we pulled from the database
$files_to_zip[$index] = $image_path . $file;
}
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 1:03 pm
by simonmlewis
Gotchya.
So posting the URL to this: /zip-saveimages.php?id=3411
Should make it give me a download for a Zip?
Code: Select all
<?php
include "dbconn.php";
/* Save as 'zip.php' etc */
/* From David Walsh's site - streamlined */
function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) return false;
foreach($valid_files as $file) $zip->addFile($file,$file);
$zip->close();
return file_exists($destination);
}
return false;
}
/* Simple function to send a file */
function sendfile( $filename=NULL, $filepath=NULL ){
if( file_exists( $filepath ) ){
if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime( date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)( filesize( $filepath ) ) );
header("Content-Disposition: inline; filename={$filename}");
header("Content-Transfer-Encoding: binary\n");
if( $file = @fopen( $filepath, 'rb' ) ) {
while( !@feof( $file ) and ( connection_status()==0 ) ) {
print( fread( $file, 1024*8 ) );
flush();
}
@fclose( $file );
}
return( ( connection_status()==0 ) and !connection_aborted() );
}
}
/* The directory you wish to scan for files or create an array in some other manner */
$target=__DIR__.'\my-archive.zip';
$id = $_GET['id'];
$query = "SELECT photo, photoprimary FROM products WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
$image_path = __DIR__ . '/images/productphotos/';
// Now we go through each file in our array
foreach ($files_to_zip as $index => $file) {
// and inject that path to the beginning of each file we pulled from the database
$files_to_zip[$index] = $image_path . $file;
}
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}
?>
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 1:08 pm
by Celauran
Looks good. The backslash in $target might be a problem once you deploy, but that's all I see at a glance. Have you tried it? Is something not working?
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 1:36 pm
by simonmlewis
running it locally, it doesn't do anything at all.
Will this work only on a live server, or on Windows as well?
Re: How do I create a ZIP file from this tutorial?
Posted: Tue Nov 17, 2015 1:37 pm
by Celauran
It must be doing something. Turn on error reporting, ideally setting it to -1, and see what sorts of errors are being generated. We can progress from there.