EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Zip Files

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Zip Files

PHP Zip Files

Introduction to PHP Zip Files

The PHP Zip files functions are used to stores the bunch of files or directory together in the compressed. The Zip is the archive file format that is used to stores the files in compressed form. The Zip archive is conveniently storing the bunch of files or and directory together in the compressed form for storing and sharing them. The PHP ZipArchive class can be used to zip and unzip the files. The PHP Zip files are not built in functions in PHP, as other built in functions are available in PHP, so we need to install the class if it is not present, if we are using the PHP 5 or other newer version, then the extensions required are free to download and install. The Zip extension requires is libzip.

To work with the Zip files we need to perform the installation as follow –

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Installation for the Linux users ( PHP 5+ ): The PHP Zip functions and library are not built in by default, so for the Linus user need to download them and to include Zip support, configuration option –with-libzip = DIR is to be used. The libzip version 0.11 is required, with 0.11.2 or later version recommended. For the Zip function to work, we have to compile PHP with –enable-zip.
  • Installation for the Windows users ( PHP 5+ ): Same as Linux, in windows also the PHP Zip functions and library are not built in by default, so for the windows user need to download and enable php_zip.dll file which is present inside of php.ini in order to use its functions. But in the PHP 5.3 this extension is built in.

Functions of PHP Zip Files

While working with Zip files we need to use some of the function as mentioned below –

  • zip_open(): This function is used to opens a ZIP file archive for reading.
  • zip_read(): This function is used to read the next file in an open ZIP file archive.
  • zip_entry_open(): This function is used to open a directory entry in the ZIP file in order for reading.
  • zip_entry_read(): This function is used to read from an open directory entry in the ZIP file.
  • zip_entry_compressedsize(): This function is used to get the compressed file size of the ZIP directory entry.
  • zip_entry_compressionmethod(): This function is used to get the compression method of the ZIP directory entry.
  • zip_entry_filesize(): This function is used to get the actual size of the file of the ZIP directory entry.
  • zip_entry_name(): This function is used to get the name of the ZIP directory entry.
  • zip_close(): This function is used to close the ZIP file archive.
  • zip_entry_close(): This function is used to close the ZIP directory entry.

Examples of PHP XML into Array

Example of PHP Zip files to create the Zip file-

Next, we write the PHP code to understand the PHP Zip files more clearly with the following example, where the Zip file is used to store the bunch of files and directory together in the compressed form, as below –

Example #1

First, we will create the folder and then create the files or directory which we want to bundle and Zip them, then we use the PHP code to Zip them.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,678 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
// give the path of directory whose file we want to Zip
$dir_path = "C:/xampp/htdocs/programs/";
// give the name to create the zipped directory
$zipfile = "ExZip.zip";
// Creating the new zip class
$zip_obj = new ZipArchive;
if( $zip_obj -> open( $zipfile, ZipArchive::CREATE ) === TRUE )
{
// open the directory at the gien path into the variable
$dir = opendir( $dir_path );
while( $file = readdir( $dir )) {
if( is_file( $dir_path.$file )) {
$zip_obj -> addFile( $dir_path.$file, $file );
}
}
$zip_obj ->close();
}
?>

Output:

PHP Zip Files-1.1

As in the above code it is having the directory path(from where all the files are to Zip) and the file name(by what name the zip file is to be create). First the ZipArchive class object is created by using the this code “$zip_obj = new ZipArchive;”, then iterate all the file available in that directory path and then by using ZipArchive object the files are added to the Zip archive by using this code “$zip_obj -> addFile( $dir_path.$file, $file );”.

Example #2

Example of PHP Zip files to Unzip the Zip file-

Next, we write the PHP code to understand the PHP Zip files, where the Zip file is unzip to extract all the zipped files or directory here, as below –

Code:

<?php
// Enter the path of directory
$dir_path = "C:/xampp/htdocs/programs/";
// give the name to create the zipped directory
$zipfile = "ExZip.zip";
// create new zip class object
$zip_obj = new ZipArchive;
$file = $dir_path.$zipfile;
print("The Zip file location is : ");
print($file);
// Add zip filename which we need to unzip
$zip_obj->open( $file );
// Extract all the Zip files to the current directory
$zip_obj->extractTo('./');
$zip_obj->close();
?>

Output:

PHP Zip Files-1.2

As in the above code it is having the directory path and the file name(which file is to unzip). First the ZipArchive class object is created by using the this code “$zip_obj = new ZipArchive;”, then by using ZipArchive object the file is open from that path and then extract all the files which were added to the Zip archive by using this code “$zip_obj -> extractTo(‘./’);”.

Recommended Articles

This is a guide to PHP Zip Files. Here we also discuss the introduction and functions of php zip files along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PHP mail()
  2. PHP XML Reader
  3. PHP Stack Trace
  4. PHP XML Parser

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training Course Learn More