Unzip file in server using PHP ZipArchive

Once i wanted to upload WordPress folder which contains 1000’s of files, if i choose to upload those files using FTP it will take much time.So i decided to upload it as zip file and extract it using php code. After googling i got a good source, how to extract / unzip file in server.

Follow the steps to unzip file in server:

  1. Create a file [root-dir(or)your-preferred-dir]/unzip-file.php in your server.
  2. Copy/paste below code in unzip-file.php and save it.
     $zip = new ZipArchive;
    $res = $zip->open('wordpress.zip');//File name in server.
    if ($res === TRUE) {
    $zip->extractTo('blog/');//Destination directory
    $zip->close();
    echo 'successfully extracted';
    } else {
    echo 'failed';
    }
  3. Upload your zip file in the same folder http://www.example.com/file.zip or your prefered folder as mentioned in above code.

Now run http://www.example.com/unzip-file.php file, your zip file will be extracted to the destination folder.
Is this post useful? Please comment below šŸ™‚

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: