www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: downloading folder from server

  1. #1
    Join Date
    Jun 2009
    Posts
    19

    downloading folder from server

    im having some trouble trying to download a folder from the server. im trying to create a zip file and add the contents of a folder to it. the folder i want to download would not be a static folder it would be depend on what is typed into a text box. does anyone know how to do this, because the code i currently have is not giving me an error message. it starts to open the dialog box to save but it stops half way and displays only a blank page.

  2. #2
    Join Date
    Jun 2009
    Posts
    19
    here is the code ive used for the zip.php

    <?
    $filename = $_GET["filename"];
    $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;
    ?>
    it doesnt open anything just a blank page

  3. #3
    Join Date
    Jun 2009
    Location
    Rwp, Pakistan
    Posts
    16
    u need to mention the detail clearly. Step by step.
    secondly folder can't be downloaded directly, u can download the zip file.

  4. #4
    Join Date
    Jun 2009
    Posts
    19
    Quote Originally Posted by sayfrndship1234 View Post
    u need to mention the detail clearly. Step by step.
    secondly folder can't be downloaded directly, u can download the zip file.
    i know that but i cant find any code that will create the zip file and then let me download it. i need it to zip the folder name that i type into a text box, then let me save it on my local machine.

  5. #5
    Join Date
    Jun 2009
    Posts
    19
    ok getting closer to fixing myself.

    right now the code i have opens the dialog box to save the zip file and it even lets me choose the file via a text box input that i want to save. some progress.
    alas this is not the last of it.
    now when i do save the file it doesn't automatically add the .zip file extension.
    if i add the file extension and then try to open it it says the file is corrupt, if i try to extract the data it says the file is empty.
    if anyone knows how to fix would be most grateful. i will post corrected code in here when i get it to work.

    code is as follows
    this is the form from main page
    Code:
    <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;
    	?>
    zip3.php code
    Code:
    <?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; 
    ?>

  6. #6
    Join Date
    Jun 2009
    Posts
    19
    ok so i figured out how to fix the .zip problem. if i change this line

    Code:
      header("Content-Disposition: attachment; filename=$filename");
    to this
    Code:
    	header("Content-Disposition: attachment; filename=$filename.zip");
    it saves it automatically as a zip.

    as for the other problem it still exists. i think it is because there is no code in there to actually add the files in that folder to the zip folder but i dont know how that is supposed to be worded or where to put the code.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    http://php.net/zip

    Always check the manual for answers. A quick search (typing php.net/zip actually) found this entire documentation set for creating zip files.

  8. #8
    Join Date
    Jun 2009
    Posts
    19
    Quote Originally Posted by criterion9 View Post
    http://php.net/zip

    Always check the manual for answers. A quick search (typing php.net/zip actually) found this entire documentation set for creating zip files.
    yes have seen that page. but it doesnt tell me where to insert the code and it only lets you add 1 file. there is not part about how to insert an unknown number of files. im working with images so it has to be done in binary form if im understanding correctly because it will corrupt the images.

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    If you are using linux you can just use the zip command line syntax to add whatever files you wish. You can create a temporary zip file (or more lengthy one if you are planning to only include the entire folder) and then serve the resulting zip to the user. As for only being able to add one file you just repeat the code to add the second and subsequent files. Or better yet use a loop from the result of a DIR (http://www.php.net/manual/en/class.dir.php).

  10. #10
    Join Date
    Jun 2009
    Posts
    19
    going through the code i understand that this part is only to force the dialog box to open
    Code:
     $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?
    Code:
    $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?
    Last edited by neekworld; 07-03-2009 at 02:32 PM.

  11. #11
    Join Date
    Jul 2009
    Posts
    4

    Any Update on this?

    Hello all I am trying to accomplish this also but I do not know php. If you have solved this or know where to find a tutorial that could help that would be great. I did visit the manual but its kinda difficult to understand with my limited skill set. thanks in advance.

  12. #12
    Join Date
    Jul 2009
    Posts
    2

    Dynamic folder contents

    To get a list of the contents of a directory check out http://php.net/manual/en/function.readdir.php

  13. #13
    Join Date
    Jul 2009
    Posts
    4

    So I'm getting an error message - can someone help?

    Parse error: syntax error, unexpected ':', expecting '(' in /homepages/28/d118892826/htdocs/zspot/ziptest.php on line 45

    here is my code:

    [ php ]
    <?php

    if ($handle = opendir('upload_test\xyzuser@somedoman.com')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
    echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
    echo "$file\n";
    }

    closedir($handle);
    }

    //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;
    }
    }
    $destination = file_get_contents($result);

    /* 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($destination));
    header("Content-Disposition: attachment; filename=$filename.zip");

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

    ?>
    [ /php ]

  14. #14
    Join Date
    Jun 2009
    Posts
    19
    Quote Originally Posted by CircktBrkr View Post
    To get a list of the contents of a directory check out http://php.net/manual/en/function.readdir.php
    im not trying to get a list of the contents of the folder. i want to select a folder depending on the username entered into a text box. then zip that folder and save it to the desktop. i know that you can list the contents of the folder. i saw that in the php manual too. the script that im using now is zipping the folder and saving it but it saves it back to the server. also it is only putting the files in the zipped folder that i hard code into the script.

  15. #15
    Join Date
    Jun 2009
    Location
    Rwp, Pakistan
    Posts
    16
    when u zip folder is saved on server. just give a link to that location. and for hardcode u can easliy change it into dynamic one. just replace hard code to code take will input [source] and then this input to that hard code location.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles