Functions of the ziparchive function in PHP

Source: Internet
Author: User
Tags ziparchive
This article mainly introduces the function of the ziparchive function in PHP, the interested friend's reference, hope to be helpful to everybody.

First, unzip the zip file

$zip =new ziparchive;//Create a new Ziparchive object   if ($zip->open (' Test.zip ') ===true) {   $zip->extractto (' Images ');//Assume that the extract is within the current path under the Images folder   $zip->close ();//close processed zip file}

Compress the file into a zip file

$zip =new ziparchive; if ($zip->open (' Test.zip ', ziparchive::overwrite) ===true) {   $zip->addfile (' image.txt ');// Assume that the file name is Image.txt, under the current path   $zip->close ();}

Third, file append content to zip file

$zip =new ziparchive; $res = $zip->open (' Test.zip ', ziparchive::create); if ($res ===true) {   $zip->addfromstring (' test.txt ', ' file content goes here ');   $zip->close ();   echo ' OK '; }else{   Echo ' failed ';}

Iv. Package a folder into a zip file

function Addfiletozip ($path, $zip) {   $handler =opendir ($path);//Open the current folder is specified by $path. While   (($filename =readdir ($handler))!==false) {     if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them       if (Is_dir ($path. ") /". $filename)) {//If an object being read is a folder, the recursive         addfiletozip ($path." /". $filename, $zip);       } else{//Add files to the Zip object         $zip->addfile ($path. " /". $filename);   }}} @closedir ($path); } $zip =new ziparchive (); if ($zip->open (' Images.zip ', ziparchive::overwrite) = = = TRUE) {   addfiletozip (' images/', $zip);//Call method, operate on the root directory to be packaged and pass the Ziparchive object to the method   $zip->close ();//Close the processed zip file}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.