Page 4 of 5
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:28 am
by simonmlewis
Code: Select all
/* The directory you wish to scan for files or create an array in some other manner */
$target=__DIR__.'\\MyZipArchive.zip';
echo var_dump($target);
$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\\';
echo var_dump($image_path);
// 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 );
}
This produces:
[text]string(51) "C:\xampp\phpMyAdmin\site\MyZipArchive.zip" string(56) "C:\xampp\phpMyAdmin\site\images\productphotos\" [/text]
So the paths are correct.
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:29 am
by simonmlewis
Code: Select all
// 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;
}
echo var_dump($files_to_zip);
This produces:
[text]array(2) { [0]=> string(56) "C:\xampp\phpMyAdmin\site\images\productphotos\" [1]=> string(77) "C:\xampp\phpMyAdmin\site\images\productphotos\57875142478355571.jpg" } [/text]
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:31 am
by simonmlewis
And the last section:
Code: Select all
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 );
echo var_dump($files_to_zip, $target);
}
Produces:
[text]array(2) { [0]=> string(56) "C:\xampp\phpMyAdmin\site\images\productphotos\" [1]=> string(77) "C:\xampp\phpMyAdmin\site\images\productphotos\57875142478355571.jpg" } string(51) "C:\xampp\phpMyAdmin\site\MyZipArchive.zip" [/text]
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:32 am
by Celauran
The first entry is obviously wrong as there's no file specified. Are you getting the right values back from your query? Also, does the second value in that array exist in your file system? Finally, can the web server write to your site directory to create the zip archive?
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:40 am
by simonmlewis
Goodness me, what an idiot.
Damn thing had no gallery images.
So I have added some gallery images, and the main one was there.
Now when I run it with var_dump, I get this:
[text]array(4) { [0]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\70581465241v6.jpg" [1]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\15587545140s1.jpg" [2]=> string(56) "C:\xampp\phpMyAdmin\site\images\productphotos\" [3]=> string(77) "C:\xampp\phpMyAdmin\site\images\productphotos\57875142478355571.jpg" } string(51) "C:\xampp\phpMyAdmin\site\MyZipArchive.zip" [/text]
The first two are fine in that lot, but why is the third one with no photo in it, then the first has the pic which I think is the primary photo.
Besides that, it still doesn't work. Blank screen comes up (without the error report) and no popup to download a zip.
Is this meant to create a ZIP on the server, or just give one to download "on the fly"?
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:43 am
by Celauran
simonmlewis wrote:Now when I run it with var_dump, I get this:
[text]array(4) { [0]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\70581465241v6.jpg" [1]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\15587545140s1.jpg" [2]=> string(56) "C:\xampp\phpMyAdmin\site\images\productphotos\" [3]=> string(77) "C:\xampp\phpMyAdmin\site\images\productphotos\57875142478355571.jpg" } string(51) "C:\xampp\phpMyAdmin\site\MyZipArchive.zip" [/text]
The first two are fine in that lot, but why is the third one with no photo in it
Trailing | character in the DB field? You might want to rtrim those out before exploding. Alternately, check the resultant array and remove empty values.
simonmlewis wrote:Besides that, it still doesn't work. Blank screen comes up (without the error report) and no popup to download a zip.
Is create_zip still returning false? Is a zip file being created? Need to know where it fails before we can determine why it fails.
simonmlewis wrote:Is this meant to create a ZIP on the server, or just give one to download "on the fly"?
Yes. Both.
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:53 am
by simonmlewis
I am not sure that it's because of the trailing |, because it wasn't working beforehand, when there was no images in $row->photo anyway. But I can query I think to see if there is a | on the end, and remove it. Because yes there is one.
No Zip is being created.
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:56 am
by Celauran
simonmlewis wrote:I am not sure that it's because of the trailing |, because it wasn't working beforehand, when there was no images in $row->photo anyway. But I can query I think to see if there is a | on the end, and remove it. Because yes there is one.
create_zip would have removed that entry as invalid, but you specifically asked how it was there to begin with, so I took a stab at it.
simonmlewis wrote:No Zip is being created.
Let's check the value of $valid_files after the foreach loop in create_zip. Is it empty or does it contain values?
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 8:58 am
by simonmlewis
Just quickly, is the the best method to check for the final | and remove it... and then continue...?
Code: Select all
$id = $_GET['id'];
$query = "SELECT photo, photoprimary FROM products WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$checkline = substr($row['photo'], -1); // checking for |
if ($checkline == "|")
{
$files_to_zip = substr_replace($checkline ,"",-1);
}
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:00 am
by Celauran
simonmlewis wrote:Just quickly, is the the best method to check for the final | and remove it... and then continue...?
Code: Select all
$id = $_GET['id'];
$query = "SELECT photo, photoprimary FROM products WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$checkline = substr($row['photo'], -1); // checking for |
if ($checkline == "|")
{
$files_to_zip = substr_replace($checkline ,"",-1);
}
$files_to_zip = explode('|', $row['photo']);
$files_to_zip[] = $row['photoprimary'];
No. Use
rtrim
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:00 am
by Celauran
Code: Select all
$files_to_zip = explode('|', rtrim($row['photo'], '|'));
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:03 am
by simonmlewis
Heavens that's clever.
The answer the valid_files:
[text]array(3) { [0]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\70581465241v6.jpg" [1]=> string(73) "C:\xampp\phpMyAdmin\site\images\productphotos\15587545140s1.jpg" [2]=> string(77) "C:\xampp\phpMyAdmin\site\images\productphotos\57875142478355571.jpg" } [/text]
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:04 am
by Celauran
I'm in meetings for the next hour or so. We can pick this up later.
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:05 am
by simonmlewis
Cool thanks.
Re: How do I create a ZIP file from this tutorial?
Posted: Wed Nov 18, 2015 9:57 am
by Celauran
The array being populated is a good sign. The file paths are correct and create_zip knows the files exist. Now for some reason it's not able to create the zip file. First thing that comes to mind is write permissions, but I haven't worked with Windows in ages and don't know how permissions are implemented there.