Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Zip on the fly

  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Zip on the fly

    I have been using this script on a Linux server, and it works perfectly..
    I am now using wamp, but it only gives me an empty zip file.. any ideas why



    PHP Code:
    <?php


        
    //Get the directory to zip
        
    $filename_no_ext$_GET['directtozip'];

        
    // we deliver a zip file
        
    header("Content-Type: archive/zip");

        
    // filename for the browser to save the zip file
        
    header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");

        
    // get a tmp name for the .zip
        
    $tmp_zip tempnam ("tmp""tempname") . ".zip";

        
    //change directory so the zip file doesnt have a tree structure in it.
        
    chdir('./BillsMusic/'.$_GET['directtozip']);

        
    // zip the stuff (dir and all in there) into the tmp_zip file
        
    exec('zip '.$tmp_zip.' *');

        
    // calc the length of the zip. it is needed for the progress bar of the browser
        
    $filesize filesize($tmp_zip);
        
    header("Content-Length: $filesize");

        
    // deliver the zip file
        
    $fp fopen("$tmp_zip","r");
        echo 
    fpassthru($fp);

        
    // clean up the tmp zip file
        
    unlink($tmp_zip);
    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    I suspect it's a file path problem to be honest. Issues like this frequently crop up when switching between windows / linux machines. You need to do a few more checks such as using file_exists() rather than just assuming it's there.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts
    The path is ok as it selects the folder name correctly, I have since been told I have to change
    from system Zip command to the PHP Zip extension.

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by Feckie View Post
    The path is ok as it selects the folder name correctly, I have since been told I have to change
    from system Zip command to the PHP Zip extension.
    That is correct. In the code you posted, you are calling exec - which then executes a program which is external to PHP.

    I suggest avoiding the use of exec because it introduces portability issues, as you have discovered.

    The best thing for your needs is PHP's ZipArchive class, which is documented here: PHP: ZipArchive - Manual

    I've used it myself successfully in the past, it is pretty intuitive.

    Provided you are using a recent(ish) version of PHP, it should be bundled with your PHP build and just work out-of-the-box.


  •  

    Posting Permissions

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