Php-mysql Course

The function presented in this page can be used to Extract / Unzip ZIP archive files with PHP. The function receives two parameters: the path-name of the Zip file to open, and the directory on server where the ZIP will be extracted.
Returns an Array with the path and name of each file in archive, or error message if unable to open the ZIP archive.

- Important: If you work on Linux system, PHP must have CHMOD write permisions in the directory in which the files will be extracted.
<?php
// Function to Extract ZIP Archive. Returns an Array with the path and name of each file in archive, or error message
// receives 2 arguments: the path-name of the Zip file to open, the directory on server where the ZIP will be extracted
function extractZip($zip_file, $dir_extract) {
  // PHP-MySQL Course - https://coursesweb.net/php-mysql/
  $re_arr = array();     // will store and return the name of the files in archive

  // create ogject of ZipArchive class, and open $zip_file
  $zip = new ZipArchive();
  $res = $zip->open($zip_file);

  // if the $zip_file can be opened
  if($res === TRUE) {
    // traverse the index number of the files in archive, store in array the name of the files in archive
    for($i = 0; $i < $zip->numFiles; $i++) {
      $re_arr[] = $zip->getNameIndex($i);
    }

    // extract the files
    $zip->extractTo($dir_extract);
    $zip->close();    

    return $re_arr;
  }
  else  echo "Failed to open $zip_file , code: $res";
}

  /* Example */

// the path-name of the zip file, and directory to unzip
$zip_file = 'path/name.zip';
$dir_extract = 'dir_to_unzip';

// unzip the $zip_file, get and output the array with names of the extracted files
$files_zip = extractZip($zip_file, $dir_extract);
print_r($files_zip);
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Extract / Unzip ZIP archive files with PHP

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141763)
  2. Download PHP-MySQL resources (1156)
  3. AJAX and XML (2351)
  4. PHP PDO - Introduction and Connecting to Databases (8631)
  5. Laravel Basic Architecture (1280)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (487)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (73)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)