Have you ever wondered how to zip a file on a linux server using PHP script. This is very useful in cases where you don’t have access to cpanel or whm. Zipping files and folders is a common task for many users. It helps to reduce the size of the file, which makes it easier to transfer or store. With the help of PHP, we can create a script to zip files and folders on the server.
Table of Contents
In this article, we will learn how to create a PHP script that can zip files and folders on the server.
What is a Zip File?
A zip file is a compressed archive file that contains one or more files. It is used to reduce the size of the original file(s) and make it easier to transfer or store them. The compression is achieved by using a lossless data compression algorithm, which means that the original data can be restored exactly as it was before compression.
Zip files are commonly used to transfer multiple files over the internet, as they can be easily sent as a single file, reducing the time it takes to transfer and the amount of storage space required. Additionally, they can be password-protected for added security. The most common file extension for a zip file is .zip, but other extensions such as .rar, .7z, and .tar.gz are also used.
To extract the contents of a zip file, users need to unzip it using a software program designed for that purpose, such as WinZip, 7-Zip, or WinRAR. Once unzipped, the original files can be accessed and used as needed.
Related: How to Implement Auto Suggestion in WordPress Search?
Important Terminology Related to the Topic
Here’re some important terms and their definitions that you should know about:
1. PHP
PHP stands for Hypertext Preprocessor. It is a server-side scripting language that is used for web development.
2. Zipping
Zipping is a process of compressing one or more files into a single archive file, usually with a .zip extension.
3. Server
A server is a computer system that provides data or services to other computers or devices on a network.
4. Compression
The process of reducing the size of a file or folder by using a data compression algorithm.
5. Archive
A collection of files and/or folders that have been compressed into a single file.
6. Zip archive
A type of archive file that uses the .zip file extension.
7. Unzip
The process of extracting the contents of a zip archive to access the original files.
8. Lossless compression
A type of compression that does not result in any loss of data, so the original data can be restored exactly as it was before compression.
8. Lossy compression
A type of compression that results in some loss of data, and the original data cannot be restored exactly as it was before compression.
9. Encryption
The process of converting data into a coded form to protect it from unauthorized access.
10. Password-protected
A zip archive that is encrypted and requires a password to be entered before the contents can be extracted.
11. Extract
The process of removing the contents of an archive file, such as a zip file, to access the original files.
12. Archive utility
A software program used to create and extract archive files, such as WinZip, 7-Zip, or WinRAR.
Also read: How to Implement Infinite Pagination in WordPress?
How to Create the PHP Script
To create a PHP script that can zip files and folders on the server, we will be using the ‘ZipArchive’ class. This class provides us with all the necessary methods to zip and unzip files and folders.
Here’re the steps that you should use to create the PHP script:
Step 1: Create a new instance of the ZipArchive class
The first step is to create a new instance of the ‘ZipArchive class’. This can be done using the following code:
$zip = new ZipArchive;
Step 2: Create a new zip file
Next, we will use the ‘open’ method of the ‘ZipArchive’ class to create a new zip file. This method takes two arguments: the first argument is the path of the zip file and the second argument is the mode in which the zip file should be opened. The following code demonstrates how to create a new zip file:
$zip_file = 'example.zip';
if ($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
// Code to add files to the zip archive goes here
$zip->close();
echo 'Zip archive created successfully!';
} else {
echo 'Failed to create the zip archive';
}
Step 3: Add files to the zip archive
Once the zip file is created, we can add files and folders to it using the ‘addFile’ and ‘addDir’ methods of the ‘ZipArchive’ class. The ‘addFile’ method takes two arguments: the first argument is the path of the file that we want to add to the zip archive, and the second argument is the name that we want to give to the file in the zip archive. The following code demonstrates how to add a file to the zip archive:
$file = 'example.txt';
$zip->addFile($file, basename($file));
Step 4: Add folders to the zip archive
The ‘addDir’ method takes one argument, which is the path of the folder that we want to add to the zip archive. The following code demonstrates how to add a folder to the zip archive:
$folder = 'example_folder';
$zip->addDir($folder);
Step 5: Close the zip archive
Once we have added all the files and folders that we want to include in the zip archive, we can close the zip archive using the ‘close’ method of the ‘ZipArchive’ class. The following code demonstrates how to close the zip archive:
$zip->close();
It is important to note that the ‘close’ method must be called after adding all the files and folders to the zip archive. Failing to do so may result in a corrupted zip archive.
Steps to Zip Files and Folders on a Server Using PHP Script
1. Create a file “backup.php” on the root directory of the server
2. Add the following piece of script in backup.php
<?php
$foldertobackup = 'yourfoldername';
$backup = '/home/xyz/public_html/'.$foldertobackup;
$newname = $foldertobackup.'.backup.zip';
$cmd = "zip -r $newname $backup";
$compressFolder = exec($cmd ." 2>&1" );
echo $compressFolder;
if($compressFolder)
{
echo 'Done';
}else{
echo 'Not Done';
}
?>
3. Now, open your web browser like chrome or firefox and run the backup.php file.
e.g. http://yourdomainname.com/backup.php
4. Within seconds or minutes… depending upon the file size the zip file will be created on your server.
5. Download the zip file using FTP.
Related: How to Add Google Tag Manager Script on Your Website?
How to Know Your Directory Path on the Server?
Not sure, how to get the correct path of your root directory? Well, you can get it through dumping the $_SERVER variable. Use the var_dump($_SERVER); function in your script to know the detailed information.
Or, simply use the following:
$backup = $_SERVER['DOCUMENT_ROOT'] . '/' . $foldertobackup;
instead of the below piece of code
$backup = '/home/xyz/public_html/'.$foldertobackup;
Also read: How to add Cookie notice on your website
Conclusion
In this article, we learned how to create a PHP script that can zip files and folders on the server. By using the ZipArchive class, we were able to create a new zip file, add files and folders to the zip archive, and close the zip archive. This script can be useful in various situations where we need to compress files and folders before transferring or storing them. By using PHP, we can automate this process and make it easier for users.
In conclusion, zipping files and folders on a server using PHP script is a straightforward and efficient process. By following the steps outlined in this article, developers can create their own scripts to meet their specific needs. With the help of PHP and the ZipArchive class, we can compress files and folders on the server with ease.