Managing a large number of files on a web server, especially taking backups, is crucial. However, the process can be time-consuming, and sometimes files fail to download correctly. Here, we introduce a method using PHP to compress files on a web server into a zip format and then download them to your local environment. This method helps you save time and safely back up your data.
Preparation Before Backup
Before starting the backup, some preparations are necessary. First, check if there are no zip files or PHP script files with the same name in the directory you plan to compress on the web server. This is important to avoid file name conflicts.
Preparing the PHP Script for Compression
Next, prepare a PHP script (zip.php) to automate the compression task. This script compresses the specified directory into a zip file and saves it on the server. Below is the basic structure of the PHP script:
Content of zip.php
<?php
/*
This PHP script compresses a directory on a web server.
Before using, ensure that there are no zip files or PHP script files with the same name in the directory to be compressed.
Upload this PHP file to the location of the directory you want to compress on the web server, then access:
https://WEB_SERVER_URL/zip.php
to compress the specified directory into the specified zip file.
*/
$zipdr = "testdr"; // Name of the directory to compress
$zipdrpass = getcwd().'/'.$zipdr; // Path to the directory + script location
$zipfilename = 'testdr.zip'; // Name of the resulting zip file
$zipfilepass = getcwd().'/'.$zipfilename; // Path to the resulting zip file + script location
if(!is_dir($zipdrpass)){
exit("The directory to be compressed \"".$zipdrpass."\" does not exist.");
}
function zip($zipdrpass, $zipfilepass){
chdir($zipdrpass);
return shell_exec("zip -r {$zipfilepass} .");
}
if($zip = zip($zipdrpass, $zipfilepass)){
echo '<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>The directory has been compressed.</title></head><body>';
echo "The directory has been compressed.<br />\n";
echo "Compressed directory: $zipdrpass<br />\n";
echo '<div style="overflow:auto; height:300px; border: #ccc 1px solid; margin:20px;">';
echo "$zip</div>\n";
echo "$zipfilepass: was successfully compressed.<br />\n";
echo '</body></html>';
}else{
echo("Compression failed: $zipdrpass\n");
}
?>
Upload this script file to the location of the directory you want to compress on the web server.
Note: Replace “$zipdr” with the name of the directory you want to compress on the web server. Replace “$zipfilename” with your desired zip file name.
Download PHP Script File (zip.php) from GitHub
You can download the complete script from the link below. Click “Code” and then “Download ZIP”.
Executing Compression and Download
Compression
After uploading the script file, execute it via the browser. Specifically, access:
https://WEB_SERVER_URL/zip.php
This will compress the specified directory into a zip format.
Download
Once the compression is complete, download the generated zip file to your local PC. Use an FTP client or download the file directly from the browser. To download via the browser, access:
https://WEB_SERVER_URL/testdr.zip
Conclusion
Using this method, you can efficiently and reliably back up a large number of files on a web server. To keep your server data safe, make regular backups a habit.
Note: Please use this method at your own responsibility.