How to Extract/UnZip folder on FTP Server Using PHP

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:

  • One is manually unzip the archive on our local computer and upload the extracted folder via FTP.
  • Another is to create a zip file and upload this on FTP and extract it on the server using the script.

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,

Extract folder Using PHP Without FTP Connection

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.

Extract Zip Files with FTP Connection

Let’s extract or unzip a compressed folder directly on the FTP server using FTP connection.

Step 1: Establish 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.

Step 2: Change Directory

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.

Step 2: Download and Compressed Folder

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.

Step 4: Extract/Unzip the Folder

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.

Step 5: Clean Up

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);

2 thoughts on “How to Extract/UnZip folder on FTP Server Using PHP

Leave a Reply

Your email address will not be published.