Back to Top

How to Unzip file and move to destination in PHP?

unzip file and move to destination folder in php

Here, I comes with the quick article, this is small post but very effective. This article is about how to unzip folder using command line code and move that files into the path you want.

PHP provides exec command to execute command line functions. Here we will use that function to unzip files and which is very easy and one line code to unzip a files.

Following line of code will unzip file and move it from source to destination folder.

exec("unzip $src_folder -d ' . $dest_folder . '/');

Next, You have to give permissions to the folders and files you unzipped.so,Following code with change the permission of files and directories.

exec("find $dest_folder -type d -exec chmod 0777 {} +"); // d is used for directory
exec("find $dest_folder -type f -exec chmod 0777 {} +");// f is used for files.

After, giving permission to files,you can copy/move it to the directory you want.

Let’s see the code to move files.

$files = scandir($dest_folder); // Identify directories 
foreach($files as $file) {
        if (in_array($file, array(".", "..")))
            continue;
        // If we copied this successfully, mark it for deletion 
        if (copy($dest_folder.$file, $new_dest_folder.$file)) {
            $unlink_files[] = $dest_folder.$file;
        }
}

// Delete all successfully-copied files 
foreach ($unlink_files as $unlink_file) {
 unlink($unlink_file);
}

That’s it.You can use code and put your comment if any query.
Hope this article helpful to you.As always, thanks for reading an article. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

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

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

Most Popular Posts

To Swap Database Row in PHP

Posted on 7 years ago

Bhumi

How to use Triggers in MySQL

Posted on 7 years ago

Bhumi

The Readers’ Poll – November 2012

Posted on 6 years ago

Bhumi

Hello world

Updated 1 week ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi