Page 3 of 5

Re: How do I create a ZIP file from this tutorial?

Posted: Tue Nov 17, 2015 1:52 pm
by simonmlewis
Display Errors is set to on, bt I see nothing about Error Reporting or a 0 or -1...?? in php.ini

Re: How do I create a ZIP file from this tutorial?

Posted: Tue Nov 17, 2015 1:52 pm
by Celauran
For now, you can just put it at the top of this file.

Code: Select all

error_reporting(-1);
If you've got a file that gets called on every request, that would be an even better place for it.

Re: How do I create a ZIP file from this tutorial?

Posted: Tue Nov 17, 2015 2:09 pm
by simonmlewis
[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.

Re: How do I create a ZIP file from this tutorial?

Posted: Tue Nov 17, 2015 2:16 pm
by Celauran
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.

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 3:09 am
by simonmlewis
Sorry, remind me how to var_dump.
I know it's something like:
var_dump($query);.... and then you echo it how?

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 3:33 am
by simonmlewis
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?

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 4:47 am
by simonmlewis
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);

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 5:40 am
by simonmlewis
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.

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 );
    }

?>

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 6:51 am
by Celauran
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?

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 7:09 am
by simonmlewis
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.

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 7:40 am
by simonmlewis
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

Code: Select all

$result=create_zip( $files_to_zip, $target, true );
So it's:

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 );
    }
I get this... which may give u some clues...
[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);
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

Code: Select all

$image_path = __DIR__ . '/images/productphotos/';
to be

Code: Select all

$image_path = __DIR__ . '\images\productphotos\';
But the rest of the page goes green, as though it's wrong.

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 8:05 am
by Celauran
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?

Posted: Wed Nov 18, 2015 8:09 am
by Celauran
simonmlewis wrote:If i place it under

Code: Select all

$result=create_zip( $files_to_zip, $target, true );
So it's:

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 );
    }
I get this... which may give u some clues...
[text]bool(false) [/text]
Why would it be false??
Let's take a look at the function, then.

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;
    }
It returns false if the zip file specified already exists and overwrite is turned off

Code: Select all

if(file_exists($destination) && !$overwrite) { return false; }
That's not the case here as you have set overwrite to true in your function call.

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;
        }
Start by fixing the directory separator issue. If that doesn't resolve it, you'll need to look at your $image_path and ensure that's correct.

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 8:19 am
by simonmlewis
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.

Re: How do I create a ZIP file from this tutorial?

Posted: Wed Nov 18, 2015 8:22 am
by Celauran
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.