How do I create a ZIP file from this tutorial?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
Display Errors is set to on, bt I see nothing about Error Reporting or a 0 or -1...?? in php.ini
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I create a ZIP file from this tutorial?
For now, you can just put it at the top of this file.
If you've got a file that gets called on every request, that would be an even better place for it.
Code: Select all
error_reporting(-1);-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
[text]Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\phpMyAdmin\site\dbconn.php on line 9[/text]
It's being fussy - but this is the only error.
It's being fussy - but this is the only error.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I create a ZIP file from this tutorial?
Interesting. You can turn that back off, I guess, or exclude E_DEPRECATED. Going to need to start step debugging now. Confirm that you're getting the expected results from your query, that the array is being populated correctly, that the zip file is being created, and so forth. var_dump and exit one step at a time if need be.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
Sorry, remind me how to var_dump.
I know it's something like:
var_dump($query);.... and then you echo it how?
I know it's something like:
var_dump($query);.... and then you echo it how?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
Query to DB
[text]string(58) "SELECT photo, photoprimary FROM products WHERE id = '3411'" [/text]
$row = mysql_fetch_assoc($result);
[text]resource(6) of type (mysql result) [/text]
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 );
}
[text]string(49) "C:\xampp\phpMyAdmin\site\my-archive.zip" [/text]
What else do you need to see?
[text]string(58) "SELECT photo, photoprimary FROM products WHERE id = '3411'" [/text]
$row = mysql_fetch_assoc($result);
[text]resource(6) of type (mysql result) [/text]
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 );
}
[text]string(49) "C:\xampp\phpMyAdmin\site\my-archive.zip" [/text]
What else do you need to see?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
Is the highlighted section the problem?
Code: Select all
/* The directory you wish to scan for files or create an array in some other manner */
IS THIS THE PROBLEM >>>> $target=__DIR__.'\MyZipArchive.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 );
}
echo var_dump($target);Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
This is it in full.
As I say, nothing happens, no errors. Blank screen. I'm using it via the URL with id=$id, and the var_dump shows it is getting the info.
As I say, nothing happens, no errors. Blank screen. I'm using it via the URL with id=$id, and the var_dump shows it is getting the info.
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__.'\MyZipArchive.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 );
}
?>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I create a ZIP file from this tutorial?
Is the query returning values? Do the file paths specified in $files_to_zip exist? Is the zip being created? What is create_zip returning?
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
Ok, if you can tell me how to answer those - ie. where to put var_dump to express them, I'll let you know, and hopefully we can make this work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
If I put vardump below the target, I get this for the files_to_zip
[text]array(2) { [0]=> string(56) "C:\xampp\phpMyAdmin\site/images/productphotos/" [1]=> string(77) "C:\xampp\phpMyAdmin\site/images/productphotos/57875142478355571.jpg" } [/text]
Interesting how it's \...\..\... then..../.../../
If i place it under
So it's:
I get this... which may give u some clues...
[text]bool(false) [/text]
Why would it be false??
Also, this:
Produces this:
[text]string(56) "C:\xampp\phpMyAdmin\site/images/productphotos/" [/text]
So maybe there is a problem with the \.../ going on. I did try to change
to be
But the rest of the page goes green, as though it's wrong.
[text]array(2) { [0]=> string(56) "C:\xampp\phpMyAdmin\site/images/productphotos/" [1]=> string(77) "C:\xampp\phpMyAdmin\site/images/productphotos/57875142478355571.jpg" } [/text]
Interesting how it's \...\..\... then..../.../../
If i place it under
Code: Select all
$result=create_zip( $files_to_zip, $target, true );Code: Select all
if( $target ) {
/* Zip the contents: use `true` to overwrite existing zip */
$result=create_zip( $files_to_zip, $target, true );
echo var_dump($result);
/* Send the file - zipped! */
if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target );
}[text]bool(false) [/text]
Why would it be false??
Also, this:
Code: Select all
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($image_path);[text]string(56) "C:\xampp\phpMyAdmin\site/images/productphotos/" [/text]
So maybe there is a problem with the \.../ going on. I did try to change
Code: Select all
$image_path = __DIR__ . '/images/productphotos/';Code: Select all
$image_path = __DIR__ . '\images\productphotos\';Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I create a ZIP file from this tutorial?
If you use \, it's going to break on deploy because Linux uses /, which is why I suggested using DIRECTORY_SEPARATOR. (You can define(DS, DIRECTORY_SEPARATOR) to save on some typing). Additionally, \ is an escape character, so you need \\ to get a literal backslash, hence why your editor freaks out.
Re: How do I create a ZIP file from this tutorial?
Let's take a look at the function, then.simonmlewis wrote:If i place it underSo it's:Code: Select all
$result=create_zip( $files_to_zip, $target, true );I get this... which may give u some clues...Code: Select all
if( $target ) { /* Zip the contents: use `true` to overwrite existing zip */ $result=create_zip( $files_to_zip, $target, true ); echo var_dump($result); /* Send the file - zipped! */ if( $result ) call_user_func( 'sendfile', 'MyZipArchive.zip', $target ); }
[text]bool(false) [/text]
Why would it be false??
Code: Select all
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;
}Code: Select all
if(file_exists($destination) && !$overwrite) { return false; }The other return false is when there are no valid files.
Code: Select all
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I create a ZIP file from this tutorial?
So do I have to tell it how I am defining a directory separate??
ie. what __DIR__ means?
From what you are saying, it sounds like it's doing nothing as it doesn't know where the files are... because the file string is messed up.
ie. what __DIR__ means?
From what you are saying, it sounds like it's doing nothing as it doesn't know where the files are... because the file string is messed up.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I create a ZIP file from this tutorial?
It's not that it isn't doing anything, it's taking the array you passed, checking if each file exists, and returning false because none of the files exist.
__DIR__ refers to the directory the current file is in.
http://php.net/manual/en/language.const ... efined.php
This is partly why I suggested defining key directories during application startup; it's all relative to the same starting location.
To debug this, I'd check the value of $image_path (echo, var_dump, whatever) and adjust that until it matches the correct full path on your machine.
__DIR__ refers to the directory the current file is in.
http://php.net/manual/en/language.const ... efined.php
This is partly why I suggested defining key directories during application startup; it's all relative to the same starting location.
To debug this, I'd check the value of $image_path (echo, var_dump, whatever) and adjust that until it matches the correct full path on your machine.