PHP

How to Compress, Upload, and Extract Large Files to a Web Server Using PHP

This time, we will introduce a technique for uploading large files to a web server. When installing CMS like WordPress or EC-CUBE on a web server, you often need to upload many files. This process is not only time-consuming but may also lead to issues where some files fail to upload correctly. As a solution, we will explain how to compress files into a single zip file, upload it, and extract it on the server.

Preparation Before Upload

Before uploading files to a web server, it is important to check that there are no files or directories with the same name as the zip file or PHP script you are uploading. Failing to do so may result in overwriting existing files, potentially leading to unexpected troubles.

Upload and Extraction Process

First, compress the directory you want to upload into a zip format. Then, upload this zip file along with a specific PHP script (described below) to the web server. This method helps reduce upload time and minimizes the risk of individual files failing to upload.

Preparing the PHP Script for Extraction

To automate the extraction process, prepare a specific PHP script file and upload it to the server. This script will handle the extraction of the uploaded zip file on the server.

Below is the content of the extraction PHP script file (unzip.php) to upload to the web server.

Content of unzip.php

<?php
/*
This is a PHP script for extracting a zip file on a web server.
Before using this script, ensure there are no files or directories with the same name as the zip file on the web server.
Upload the zip file and this PHP file to the same directory on the server and access:
https://WEB_SERVER_URL/unzip.php
to extract the uploaded zip file.
*/

$zipfilename = 'assyuku.zip'; // ← Change to the name of the zip file to extract (assyuku.zip)
$zipfilepass = getcwd().'/'.$zipfilename;

if(!is_file($zipfilepass)){
  exit("The zip file is not found at ".$zipfilepass.".");
}

function unzip($zipfilepass){
  return shell_exec("unzip $zipfilepass");
}

if($zip = unzip($zipfilepass)){
  echo '<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>The zip file has been extracted.</title></head><body>';
  echo "The zip file has been extracted.<br />\n";
  echo "Extracted file: $zipfilename<br />\n";
  echo '<div style="overflow:auto; height:450px; border: #ccc 1px solid; margin:20px;">';
  echo "$zip</div>\n";
  echo "$zipfilename: successfully extracted.<br />\n";
  echo '</body></html>';
}else{
  echo("Extraction failed: $zipfilepass\n");
}

?>

This script (unzip.php) is used to directly extract the zip file on the server. Replace assyuku.zip with the name of the zip file you want to extract.

Download the Extraction PHP Script File (unzip.php) from GitHub

Click “Code” on the linked page and select “Download ZIP” to download the entire package.

Download from “yo1tec/unzip” >>

Execution and Results: Accessing the Extraction PHP Script File (unzip.php)

By accessing the uploaded extraction PHP script (unzip.php) via a web browser, the uploaded zip file will be automatically extracted on the server. This process allows you to deploy large amounts of files to the web server easily and quickly.

Access the following in your browser:

https://WEB_SERVER_URL/unzip.php

Conclusion

Using this method, you can efficiently deploy projects containing a large number of files to a web server. This streamlines the time-consuming file upload process and helps to achieve a smoother launch of websites or applications.

We hope this article provides useful information for engineers and those involved in web operations. Let’s aim to build a better web environment through technical challenges and creative efforts.

※ Please use this method at your own risk.