How does PHP implement decompression package files to the specified directory? (pure Code)

Source: Internet
Author: User
This article brings you the content is about how PHP implementation of the decompression package files to the specified directory? (pure code), there is a certain reference value, the need for friends can refer to, I hope to help you.

$SRC _file is the file path, the upload file returns the compressed package path to public function unzip ($src _file, $dest _dir=false, $create _zip_name_dir=true, $        Overwrite=true) {$filePath _arr = []; if ($zip = Zip_open ($src _file)) {if ($zip) {$splitter = ($create _zip_name_dir = = = True)?                "." : "/"; if ($dest _dir = = = False) {$dest _dir = substr ($src _file, 0, Strrpos ($src _file, $splitter)). "                /";                }//If there is no create target extract directory $this->create_dirs ($dest _dir); Unzip each file while ($zip _entry = Zip_read ($zip)) {//The file is not in the root directory $pos _las                    T_slash = Strrpos (Zip_entry_name ($zip _entry), "/"); if ($pos _last_slash!== false) {//Create directory at end with/$this->create_dirs ($dest _d                    Ir.substr (Zip_entry_name ($zip _entry), 0, $pos _last_slash+1)); }//Open Package if (Zip_entry_open ($zip, $zip _Entry, "R")) {//file name is saved on disk $file _name = $dest _dir.zip_entry_name ($zip _entry); Check if the file needs to be rewritten if ($overwrite = = = True | | $overwrite = = = False &&!i S_file ($file _name)) {//reads the contents of the compressed file $fstream = Zip_entry_read ($zip _ent                            Ry, 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);            }}//Close compressed package zip_close ($zip);        }}else{return false;  } return $filePath _arr; Returns all file paths after compression}/** * 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); }            }        }    }
Related Article

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.