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 - http://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 adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Extract / Unzip ZIP archive files with PHP

Last accessed pages

  1. Inheritance - Parent class and Child class (1109)
  2. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (27235)
  3. Get the value of multiple selected checkboxes with same name (4347)
  4. Add, Change, and Remove Attributes with jQuery (21290)
  5. Common PHP Errors and Solutions (4654)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (43787)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (33177)
  3. Read Excel file data in PHP - PhpExcelReader (33092)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (29923)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (27235)