Jump to content

Downloading zip file


neekworld

Recommended Posts

ok i have searched this site as well as 4 others looking for help on this. I am trying to download an entire folder based on a username that is input into a text box. i want to zip the file first as it will be holding images. then save the zipped file to my local machine. the current script i have is put together from 2 other scripts that i found online, sorry i dont remember now where exactly i got them from as i have changed them numerous times and i have about 6 variations now.

the script i have does exactly what i want with 2 exceptions.

1. it saves the zipped file to the server not to my local machine

2. it only added the files that i hard code into the zip.php script. not the folder that i put into the text box.

 

this is the script im using. i added the headers to force the dialog box to pop up so i can save it where i want to, but if anyone knows a better way i would be most grateful.

<?php
$files_to_zip = array(
    'upload_test/user/2009-07-01-214005Logo.jpg',
    'upload_test/user/2009-07-01-215314orange_bg.gif',
    'upload_test/user/2009-07-01-215326emptycart.gif',
    'upload_test/user/2009-07-01-223317dash.gif',
    'upload_test/user/2009-07-02-020536arrow.gif',
    'upload_test/user/2009-07-02-020717check_green.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'test.zip');

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done!
        $zip->close();
         
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}
  $buffer = file_get_contents($file);

/* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename.zip");

    /* Send our file... */
    echo $buffer;   

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.