Creation of zip folder in php
        Posted  
        
            by 
                Kishan
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Kishan
        
        
        
        Published on 2013-10-25T21:50:55Z
        Indexed on 
            2013/10/25
            21:53 UTC
        
        
        Read the original article
        Hit count: 281
        
I am trying to create a zip folder of my downloaded images. Here is my code. I am not getting any errors, but the zip is not getting downloaded.The code is getting compiled and I am getting the output till the display part of the current directory, but after that the code seems to go wrong somewhere and I am not able to get any Zip archive.
<?php
$conn_id=ftp_connect("localhost") or die("Could not connect");
ftp_login($conn_id,"kishan","ubuntu");      //login to ftp localhost
echo "The current directory is " . ftp_pwd($conn_id); //display current directory
ftp_chdir($conn_id,'/var/www/test1'); //changing to the directory where my images are downloaded.
echo "<br/><p> Changing to directory" . ftp_pwd($conn_id);
$file_folder=".";
echo "<br/> The content of the directory is <br/>";
print_r(ftp_rawlist($conn_id,".")); // display the contents of the directory
if(extension_loaded('zip'))     //check whether extension is loaded
{
$zip=new ZipArchive();  
$zip_name="a.zip";      //some name to my zip file
if($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE)
    {
    $error="Sorry ZIP creation failed at this time";
    }
$contents=ftp_nlist($conn_id,".");
foreach($contents as $file) //addition of files to zip one-by-one
    {
    $zip->addFile($file_folder.$file);
    }
$zip->close();      //seal the zip
}
if(file_exists($zip_name))  //Content-dispostion of my zip file
{
header('Content-type:application/zip');
header('Content-Disposition:attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}
?>
        © Stack Overflow or respective owner