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
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    51
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Creating Zip Files on the Fly

    hello

    I am trying to create a zip file from selections to download but for some reason the download starts but then hangs or it doesn't even download. Can anyone help me.

    PHP Code:

    for($i=1$i<=$cnt$i++) {
      
    $dwnfiles[$i] = $_POST['imgfile'.$i];
    }

    $zipName="amigo.zip";

    include_once(
    "../zipfiles/CreateZipFile.inc.php");
    $createZipFile=new CreateZipFile;
    $outputDir="../zipfiles/";

    // Code to Zip a single file
    foreach ($dwnfiles as $filer) {
      
    $filername "../".$filer;
      
    $filess explode("/"$filer);
      
    $cfile end($filess);
      
    $fileContents=file_get_contents($filername);
      
    $createZipFile->addFile($fileContents$outputDir.$cfile);
    }


    $rand=rand(1,3);
    $zipName=$rand."_".$zipName;
    $fd=fopen($zipName"wb");
    $out=fwrite($fd,$createZipFile->getZippedfile());
    fclose($fd);
    $createZipFile->forceDownload($zipName);
    @
    unlink($zipName); 

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    i have no idea how your script suppose to work, but here's basic example how to do it:
    PHP Code:
    <?php
    $zip 
    = new ZipArchive;
    if (
    $zip->open('test.zip') === TRUE) {
        
    $zip->addFile('/path/to/file1.txt''file1.txt');
        
    $zip->addFile('/path/to/file2.txt''file2.txt');
        
    $zip->addFile('/path/to/file3.txt''file3.txt');
        
    ///and so on...
        
    $zip->close();
        
    header('Content-type: application/zip');
        
    header("Content-length: " filesize('test.zip'));
        
    header('Content-Disposition: attachment; filename="test.zip"');
        
    readfile('test.zip');
    } else {
        echo 
    'error!';
    }
    unlink('test.zip');
    ?>

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    51
    Thanks
    9
    Thanked 0 Times in 0 Posts
    That is pretty much the same thing but going one file at a time but I need it to loop through an array to add the files into the zip file.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The difference is the object in use. The one in your OP is a custom object, whilst patryk's is a built in (5.2+/pecl zip 1.1.0+) class.
    You can loop it after this:
    PHP Code:
    if ($zip->open('test.zip') === TRUE)
    {
        foreach (
    $dwnfiles as $filer)
        {
            
    $zip->addFile('../' $filerbasename($filer));
        }
        
    $zip->close();
        
    header('Content-type: application/zip');
        
    header("Content-length: " filesize('test.zip'));
        
    header('Content-Disposition: attachment; filename="test.zip"');
        
    readfile('test.zip');
    }
    unlink('test.zip'); 
    Its a pity that the zip archive only works with a string, otherwise you can create a stream and not worry about having a zip file sticking around needing to be deleted.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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