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 can be used to create input text field in web page?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Which CSS property displays the text in a small-caps font?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
What instruction displays a notice box with a message inside it, in JavaScript?
for() Date() alert()
var msg = "Visit CoursesWeb.net";
alert(msg);
Indicate the PHP code used to get the users IP.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
Extract / Unzip ZIP archive files with PHP

Last accessed pages

  1. Using server-sent events - EventSource (1310)
  2. SHA256 Encrypt hash in JavaScript (28516)
  3. Area and Perimeter Calculator for 2D shapes (9806)
  4. Add Text in Canvas from Input text field, as it is Typed (9488)
  5. Add Pause in JavaScript script (14836)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (804)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (565)
  3. SHA1 Encrypt data in JavaScript (430)
  4. Create simple Website with PHP (397)
  5. Read Excel file data in PHP - PhpExcelReader (389)