How do I create a ZIP file from this tutorial?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

How specifically you would do that would depend on how your app is structured, but look at things like __DIR__, dirname(), etc.
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?

Post 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:

Code: Select all

    $storage=__DIR__.'\location';
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 ...??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis wrote:

Code: Select all

$storage=__DIR__.'\location';
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.
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?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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;
}
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?

Post 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 );
    }
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Once you've finished creating your $files_to_zip array
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?

Post by simonmlewis »

Sorry.....?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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;
}
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?

Post 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 );
    }
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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?
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?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
Post Reply