How to extract a zip file from PHP to a specified folder _php tips

Source: Internet
Author: User
Tags chmod create directory php file php regular expression regular expression

This article illustrates how PHP extracts a zip file to a specified folder. Share to everyone for your reference, specific as follows:

/** * Function: Extract zip Format files * author:friker * date:2015-15-14 * reference:http://php.net/manual/zh/ref.zip.php * All Rights reserved:wujiangwei123@126.com */class unzip{Public function __construct () {//init code here ... Heade
  R ("Content-type:text/html;charset=utf8"); /** * Extract files to specified directory * * @param string Zip compressed file path * @param string extract the destination of the file * @param boolean to create the target text with the name of the compressed file Folder * @param Boolean overrides files that already exist * * @return Boolean returns success or failure/Public function unzip ($src _file, $dest _dir=fals E, $create _zip_name_dir=true, $overwrite =true) {if ($zip = Zip_open ($src _file)) {if ($zip) {$splitter = ($crea Te_zip_name_dir = = True)?
      "." : "/"; if ($dest _dir = False) {$dest _dir = substr ($src _file, 0, Strrpos ($src _file, $splitter)). "
      /";
       //If there is no create target decompression directory $this->create_dirs ($dest _dir); Extract per file while ($zip _entry = Zip_read ($zip)) {//File not in root $pos _last_slash = Strrpos (zip_enTry_name ($zip _entry), "/"); if ($pos _last_slash!== false) {//Create directory at end with/$this->create_dirs ($dest _dir.substr (zip_entry_na
          Me ($zip _entry), 0, $pos _last_slash+1)); //Open Package if (Zip_entry_open ($zip, $zip _entry, "R")) {//filename saved on disk $file _name = $
            Dest_dir.zip_entry_name ($zip _entry);
              Check to see if the file needs to overwrite if ($overwrite = = True | | $overwrite = = False &&!is_file ($file _name)) {
              Read the contents of the compressed file $fstream = Zip_entry_read ($zip _entry, zip_entry_filesize ($zip _entry));
              @file_put_contents ($file _name, $fstream);
              Set Permissions chmod ($file _name, 0777); echo "Save:" $file _name. "
            <br/> ";
          }//Close the entrance zip_entry_close ($zip _entry);
      }///Turn off Compression pack zip_close ($zip);
    }}else{return false;
  return true;
  }/** * Create directory * *Public Function Create_dirs ($path) {if (!is_dir ($path)) {$directory _path = "";
     $directories = Explode ("/", $path);
     Array_pop ($directories); foreach ($directories as $directory) {$directory _path. = $directory.
       /";
         if (!is_dir ($directory _path)) {mkdir ($directory _path);
       chmod ($directory _path, 0777);
 }}}/* using: $z = new Unzip ();
$z->unzip ("./bootstrap-3.3.4.zip", './unzipres/', true, false);

 */

More about PHP Interested readers can view the site topics: "PHP operation zip file and compression skills summary", "PHP file Operation Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP basic Grammar Introductory Course", " Introduction to PHP object-oriented programming, "PHP string (String) Usage Summary", "Php+mysql Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.