Php-mysql Course

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.
<?php
// 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 - https://coursesweb.net/php-mysql/
  // 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){
      $zip->addFile($file);
    }
    $zip->close();
    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 = 'final.zip';

// 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));
readfile($zip_file);
exit();

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Create ZIP file archive with PHP

Last accessed pages

  1. PHP Unzipper - Extract Zip, Rar Archives (31737)
  2. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (55416)
  3. SHA1 Encrypt data in JavaScript (35321)
  4. Output or Force Download MP3 with PHP (5663)
  5. Working with MySQL Database (3058)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (250)
  2. Read Excel file data in PHP - PhpExcelReader (90)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (73)
  5. The Mastery of Love (66)
Chat
Chat or leave a message for the other users
Full screenInchide