php

How to extract or unzip a folder on FTP server in PHP

In this tutorial, we are going to see how to extract or unzip a folder on FTP server using PHP. We are going to use ZipArchive php class, you can read the full documentation from this link.

The following steps are required to extract a zip file to FTP server.

  • Step 1: Create a Zip file and upload it to FTP server.
  • Step 2: Create “extract.php” file.
  • Step 3: Copy and paste the method below on the above file.
<?php  
     $zip = new ZipArchive;  
     $res = $zip->open('archive.zip');  
     if ($res === TRUE) {  
         $zip->extractTo('dest/');  
         $zip->close();  
         echo 'ok';  
     } else {  
         echo 'failed';  
     }  
?>

In the above method, we need to replace the name “archive.zip” with the name of your target zip file and the directory “dest/” with the destination directory. In the code, we first create an instance of the ZipArchive class, then call open() method of ZipArchive class. If the zip is open, then you can use extractTo() method and finally close the zip by using “close()”.

Step 4: Run the extract.php file, you will get all extracted files in your destination directory “dest/”.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *