Updated April 18, 2023
Introduction to PHP ziparchive
ZipArchive method in PHP is used to add the file, new directory, and able to read the zip in PHP. ZipArchive is a class rather than a method; ziparchive contains multiple methods; by the use of it, we can perform various operations on the zip in PHP. ZipArchive methods can do multiple operations, which are to add a new file, to add a new directory, to close the archive, to extract the ziparchive content, to open a ziparchive in PHP. In the coming section, we will see in detail how we can use the ziparchive method in PHP.
Syntax
Now we will discuss the multiple syntaxes for ziparchive in PHP. As I told you, there is no such method as ziparchive in PHP; this is a class instead which contains several ziparchive methods inside it to access the zip and read the zip. Let’s see some syntax for a better understanding of the method see below;
1. To open ziparchive: Below is the syntax
ziparchive->open('your_file_name');
As you can see, we just have to pass the file name which we want to open. Also, these methods can be called by using the ziparchive instance.
2. To close a file: Below is the syntax
ziparchive->close();
As you can see, we just have to call the close method on ziparchive instance to close any file in PHP.
How ziparchive method work in PHP?
As know, we know that the ziparchive method is used to read the zip in PHP, but ziparchive instead of a class, not a method, but it contains several methods that can be used to perform operations on the ziparchive. So to handle ziparchive, we use ziparchive class in PHP. To use any method, we have to create the instance of ziparchive class, but with the use of it, we can easily call any method. We can perform any operations by using a ziparchive instance like open a file. Close a file, create a new file, create a new directory, read ziparchive content, and so on.
Let’s discuss each method in detail to use them in PHP see below;
- statIndex: This method is used to get the entry of the file defined by using the index.
- setCompressionIndex: This method is used to define the comparison method.
- renameName: This method is used to rename the file.
- open: This method is used to open a zip file in PHP.
- setCommentName: This method is used to comment.
- unchangeAll: This method is used to revert all the changes that have been performed to the zip archive.
- setPassword: This method is issued to set the password for the zip archive.
- close: This method is used to close the zip archive but is currently being used.
- addEmptyDir: This method is used to add a new empty directory to the zip archive.
- addFromString: This method is used to a file to the zip archive.
- count: This method is used to count the number of files present in the archive.
- deleteName: This method is used to delete an entry from the zip archive based on the name.
- addFile: This method is issued to add a file in the zip archive. But we have to specify the path as well.
- statName: This method is used to get the details of the entry based on the name.
- replaceFile: This method is used to replace the file in the zip archive based on the path.
- locateName: This method is used to get the index of the entry in the zip archive.
- getNameIndex: This method is used to get the name of the entry from the zip archive based on the index.
- getCommentName: This method is used to get the comment of the entry based on the name.
- getFromIndex: This method is used to get the contents of the entry by sing its index in a zip archive.
- extractTo: This method is used to get or extract the contents of the zip archive in PHP.
- deleteIndex: This method is used to delete the entry from the zip archive by using the index.
- setEncryptionIndex: This method is used to set the encryption for the entry in the zip archive using its index.
- setArchiveComment: This method is used to set comments for the zip archive in PHP.
- setExternalAttributesName: This method is used to set the external attribute of the entry based on its name.
- setEncryptionName: This method is used to set the encryption of the entry of the zip archive based on the name.
To use this all this method, we need to have a ziparchive instance created in our program. After that, only we can call this method to perform any operations on the zip archive in PHP. Let’s see how to create a ziparchive instance in PHP see below;
Example:
$myzip = new ZipArchive();
This is s sample for beginners to use ziparchive in php; we will see some examples in the next section to better understand the ziparchive method in PHP.
Example of PHP ziparchive
In this example, we create one file inside the zip archive; after this, we are closing this file. A sample program for beginners to understand this better.
Code:
<?php
echo 'demo for ziparchive method in PHP !!!';
$zip = new ZipArchive;
echo 'opening zip file !!';
if ($zip->open('https://cdn.educba.com/path/mydem.zip') === TRUE) {
//adding file
$zip->addFile('https://cdn.educba.com/path/myzip/demofile.txt', 'mynewdemofile.txt');
//closing file
$zip->close();
echo 'file added successfully !!';
echo 'file close successfully !!'
} else {
echo 'error occured while cerating the file.'
echo 'process failed !!'
echo 'failed while creation !!';
}
?>
Output:
Conclusion
By using ziparchive methods, we can perform many operations on the ziparchive. This is the class with so many different methods available. With the help of this, we can create a file, directory, delete, open, and close our ziparchive in PHP.
Recommended Articles
This is a guide to PHP ziparchive. Here we discuss the introduction, syntax, and working of ziparchive method in PHP along with an example and output. You may also have a look at the following articles to learn more –