This tutorial will walk you through the steps necessary to use PHP to extract/unzip a folder from an FTP server. To assist you in completing this task, we will go over the steps with an example code.
Nowadays normally we are uploading and downloading work on the server. In some cases, we need to upload a big file on the server. You may need to extract or unzip a compressed folder directly on the FTP server.
There is two option to upload files on the server:
The FTP protocol doesn’t allow the zip file extraction so we need to do it ourselves, here in this quick tutorial, I will let you know the PHP script that will extract file on FTP.
I am using ZipArchive PHP class library, You can read the full documentation and installation from here.
Also, check out other related tutorials,
There are the following steps needed to extract the zip file on FTP.
Step 1: Create a Zip file and upload it on FTP.
Step 2: Create a file extract.php
.
Step 3: Paste the below method on the extract.php
file.
<?php $zip = new ZipArchive; $res = $zip->open('corp.zip'); if ($res === TRUE) { $zip->extractTo('test/'); $zip->close(); echo 'ok'; } else { echo 'failed'; } ?>
In the above method, We need to replace the core.zip
name with your target zip file name and test/
directory with your target directory.
In code first, We create a zip class instance and then call an open()
method of the zip archive class. If the zip is openable then called the extract method and finally close the zip by using close()
.
Step 4: Upload the above extract.php
file on the server and run this file now you will get all extracted file in your target folder.
Let’s extract or unzip a compressed folder directly on the FTP server using FTP connection.
The first step is to establish a connection to the FTP server using PHP. You can achieve this using the built-in FTP functions provided by PHP.
$ftpServer = 'ftp.example.com'; $ftpUsername = 'your-ftp-username'; $ftpPassword = 'your-ftp-password'; $connId = ftp_connect($ftpServer); $login = ftp_login($connId, $ftpUsername, $ftpPassword); if ($connId && $login) { // FTP connection established } else { // Error in establishing FTP connection }
You need to replace ftp.example.com
with the hostname of your FTP server and provide the appropriate FTP username and password.
You need to navigate to the directory where the compressed folder is located. Use the ftp_chdir()
function to change the directory.
$ftpServer = 'ftp.example.com'; $ftpUsername = 'your-ftp-username'; $ftpPassword = 'your-ftp-password'; $connId = ftp_connect($ftpServer); $login = ftp_login($connId, $ftpUsername, $ftpPassword); if ($connId && $login) { $folderPath = '/path/to/compressed/folder'; if (ftp_chdir($connId, $folderPath)) { // Directory changed successfully } else { // Error in changing directory } } else { die('Error in establishing FTP connection'); }
Replace /path/to/compressed/folder
with the actual path to the folder on the FTP server.
You need to download the compressed folder from the FTP server to a temporary location on your server.
$tempFolder = '/path/to/temp/folder'; $zipFile = 'compressed-folder.zip'; if (ftp_get($connId, $tempFolder . '/' . $zipFile, $zipFile, FTP_BINARY)) { // Compressed folder downloaded successfully, extract files } else { die('Error in downloading the compressed folder') }
Replace /path/to/temp/folder
with the path to a temporary folder on your server where the compressed folder will be downloaded.
Let’s use PHP’s ZipArchive
class to extract its contents.
$zip = new ZipArchive; if ($zip->open($tempFolder . '/' . $zipFile) === TRUE) { $zip->extractTo($tempFolder); $zip->close(); // Folder extracted successfully } else { // Error in extracting the folder }
in the above code, We have created a new ZipArchive object, open the downloaded zip file, extract its contents to the temporary folder, and then closed the archive.
Finally, don’t forget to clean up the temporary files and close the FTP connection.
// Clean up the temporary files unlink($tempFolder . '/' . $zipFile); // Close the FTP connection ftp_close($connId);
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
it's not work.
I just checked, its working fine, hv u changed source folder zip name?