Jump to content

Download folder from server to a zip folder


neekworld

Recommended Posts

ok i have a question. i have spent days searching for a script that will allow me to download the entire contents of a specific folder and zip it so i can save it to my local machine. i have a script that opens the dialog box and even lets me save the file. when it saves however it does not automatically add the .zip extension to the file name. if i add it before i save it, when i try to open it it tells me the file is corrupt, if i try to extract the files it says that the contents are empty. if anyone knows how to fix this i would be most grateful

 

my code is as follows

this is the form from my main page. i am trying to allow the user to input a specific file to download and send that variable to the zip.php script. that part actually works it allows me to input a name and it selects the folder with that name on it.

<form id="form2" name="form2" method="post" action="zip3.php">

      <label>Enter Username to get images for that user<br />

      <input type="text" name="userName" id="userName" />

      </label>

      <input name="Submit2" type="submit" />

    </form>

    <?php

$userName = $_POST['userName'];

$_SESSION['userName'] = $userName;

?>

 

this is the zip3.php script

<?php

    $filename = "upload_test/".$_POST['userName'];

    $buffer = file_get_contents($filename);

 

    /* 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");

 

    /* Send our file... */

    echo $buffer;

?>

Link to comment
Share on other sites

There is no code in that snippet to zip anything. Also, this line....

 

header("Content-Disposition: attachment; filename=$filename");

 

should be....

 

header("Content-Disposition: attachment; filename=$buffer");

Link to comment
Share on other sites

ok actually if i change to this

    header("Content-Disposition: attachment; filename=$filename.zip");

 

it will put the .zip extension. but it still gives me the error about the file being corrupt when i try to open it.

i know im missing part of the code i wasnt sure how to put in the add files to the zip folder once it is created.  how do you add the files to it?

 

Link to comment
Share on other sites

going through the code i understand that this part is only to force the dialog box to open

 $buffer = file_get_contents($filename);

/* 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; 

 

so this part in theory should put the files into the zip folder right?

$files_to_zip = array(
    'upload_test/2009-07-01-214005Logo.jpg',
    'upload_test/2009-07-01-215314orange_bg.gif',
    'upload_test/2009-07-01-215326emptycart.gif',
    'upload_test/2009-07-01-223317dash.gif',
    'upload_test/2009-07-02-020536arrow.gif',
    'upload_test/2009-07-02-020717check_green.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.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;
    }
}

 

now i know the images are hard coded in there which is not what i want to do. but this gives me the same error as the first one. does anyone know what is wrong with the code. and possibly if they know how to put the files into the array dynamically?

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.