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 HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("");
echo $homepage;
Create ZIP file archive with PHP

Last accessed pages

  1. Totem Balls (735)
  2. Add, Change, and Remove Attributes with jQuery (29828)
  3. Get the value of the selected /checked checkboxes in a form (29428)
  4. Grid of Defense (415)
  5. Zuma Games (18917)

Popular pages this month

  1. Qwop (4610)
  2. Drag Racer V3 (2236)
  3. Flash Games - Free online Games (1107)
  4. Bubbles3 (993)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (983)