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 is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
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">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Extract / Unzip ZIP archive files with PHP

Last accessed pages

  1. Create simple Website with PHP (15240)
  2. Transparent Background, but not the text-content on it (1227)
  3. Ajax-PHP Chat Script (32266)
  4. Drag Racer V3 (15436)
  5. Insert, Select and Update NULL value in MySQL (17154)

Popular pages this month

  1. Qwop (7896)
  2. Drag Racer V3 (5541)
  3. Bubbles3 (2160)
  4. List with All the Games (1834)
  5. Flash Games - Free online Games (1718)