Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a Zip File using PHP
#1
Credits and Source: http://www.dreamincode.net/

Name: Create a zip file in PHP
Description: This is a snippet for creating a zip file with PHP using the ZipArchive class
Snippet:
PHP Code:
function zip_files($files_to_add = array(), $save_to ''$overwrite_existing false)
{
    
//if the file already exists and overwrite is
    //set to false then return false and get out
    
if(file_exists($save_to) && !$overwrite_existing)
    {
        return 
false;
    }
    
    
//now on to the action
    
$files = array();
    
    
//check for an array
    
if(is_array($files_to_add))
    {
        
//loop through each file in the array
        
foreach($files_to_add as $valid_file)
        {
            
//if the file exists then add to the file array
            
if(file_exists($valid_file))
            {
                
$files[] = $valid_file
            
}
            
            
//check the count of files
            
if(count($files))
            {
                
//create a new zip archive object
                
$zip_archive = new ZipArchive();
                
                
//open zip archive for modifying and check for a value
                //of true with the modes specified
                
if($zip_archive->open($save_to$overwrite_existing ZIPARCHIVE::OVERWRITE ZIPARCHIVE::CREATE) != true)
                {
                    return 
false;
                }
                
                
//loop through the file array
                //adding each file to the new zip archive
                
foreach($files as $file)
                {
                    
$zip_archive->addFile($file$valid_file);
                }
                
                
//close the zip archive
                
$zip_archive->close();
                
                
//finally make sure the new zip file exists
                
return((file_exists($save_to)) ? true false);
            }
        }
    }
}

//Example usage

$my_files = array('File1.txt','File2.txt','File3.txt');

$zip zipFiles($my_files,'MyZipFile.zip');

if(!
$zip)
{
    echo(
'There was a problem creating the zip file');
}
else
{
    echo(
'Zip file created!');


Instructions: Requirements:

PHP 4:
The bundled PHP 4 version requires » ZZIPlib, by Guido Draheim, version 0.10.6 or later

PHP 5.2.0 or later:
This extension uses the functions of » zlib by Jean-loup Gailly and Mark Adler.

Thankyou for reading. Be happy always Smile
Reply


Messages In This Thread
Create a Zip File using PHP - by zone - 11-07-2009, 03:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP edit file on server? Inventor 5 3,062 08-08-2012, 04:05 AM
Last Post: saikiran
  Use PHP in a text file? Blic 4 1,510 10-25-2011, 08:12 PM
Last Post: Project Evolution
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,668 10-10-2011, 01:00 PM
Last Post: Greyersting
  help : PHP : Executing .jar file killer_03 0 1,129 03-12-2011, 11:09 PM
Last Post: killer_03

Forum Jump:


Users browsing this thread: 1 Guest(s)