Simple PHP function that can be used to create ZIP file archive with PHP. The function receives two parameters: an array with the path-name of the files to add in archive, and a string with the path-name of the Zip file that will be created.

- Important: If you work on Linux system, PHP must have CHMOD write permisions in the directory in which the ZIP file /archive will be created.
// function to create ZIP archive. Returns TRUE on success, otherwise, False
// receives 2 arguments: an array with the path-name of the files to add in archive,
// and the path-name of the Zip file that will be created
function createZip($files, $zip_file) {
  // PHP-MySQL Course -
  // create an object of the ZipArchive class
  $zip = new ZipArchive;

  // if the $zip_file can be created, traverse the array $files and add each file in archive
  if($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
    foreach($files as $file){
    return true;
  else return false;

  /* Example */

// Array with the path-name of the files to be added in ZIP archive
$files = array('file1.txt', 'image.jpg', 'audio.mp3');

// the path-name of your final zip file on your server
$zip_file = '';

// calls the createZip() to create the ZIP archive, returns message of success or failure
if(createZip($files, $zip_file)) echo 'The '. $zip_file. ' successfully created';
else echo 'Unable to create the '. $zip_file. ' file';
If the $zip_file already exists on server, the files will be added in the in that archive, keeping also the existing files in ZIP.
- To overwrite the ZIP archive, replace CREATE with OVERWRITE .

• If you want the ZIP archive to be downloaded after it was created, use this code:
// here the code that adds the files in ZIP archive

header('Content-type: application/zip');
header('Content-disposition: filename="'. $zip_file. '"');
header('Content-length:'. filesize($zip_file));

Daily Test with Code Example

Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1"></div></body>';
$dochtml = new DOMDocument();
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    //
Create ZIP file archive with PHP

Last accessed pages

  1. Read Excel file data in PHP - PhpExcelReader (63706)
  2. JavaScript Course - Free lessons (15710)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (48156)
  4. AJAX with POST and PHP (12412)
  5. Shape Tween - Flash Animation (4051)

Popular pages this month

  1. Contact page - CoursesWeb (1955)
  2. Tabs effect with CSS (1942)
  3. Insert, Select and Update NULL value in MySQL (1036)
  4. Read Excel file data in PHP - PhpExcelReader (832)
  5. PHP getElementById and getElementsByTagName (513)