How to Unzip an Uploaded File with PHP

By | November 17, 2008

We’re often dealing with file uploads. Sometimes we need to compress the data that is uploaded to the server. Today I will tell you about simple methods that allow to extract the uploaded ZIP file with a PHP script.

let’s say we need to install WordPress. In order to save our time (as you know, it is faster to upload one big file, than 1000 smail files, equal by size to a big one), we will upload a zipped file and then will extract it using PHP.

I will not describe the process of creation of file upload form. If you’re interested in such a tutorial, please, let me know about it in comments. This article will tell you how to process your uploaded ZIP file.

if (is_uploaded_file($_FILES[‘guests’][‘tmp_name’]))
move_uploaded_file($_FILES[‘guests’][‘tmp_name’], “./”.$fishierul);
system(“unzip -qq ./”.$fishierul.” -d ./profiles/”.$_POST[‘type’]) ;
unlink (“./”.$fishierul);

In the code above the uploaded zip file is automatically renamed to Then it is moved to the working directory (current script directory in the sample). Then we’re unzipping it to a directory on our server using a system() command. Then the archive file is destroyed as we don’t need it anymore.

All the paths that are used in this script should be relative to our current folder or should start with “/”. Absolute paths are more useful and I suggest you to use them. This script was created about 2 years ago, when I didn’t use absolute paths.  But it is really a good Idea.

You may need to use man unzip to find out other options that are available for this command. -d shows the directory to extract files, and there are much more other options that you might need.

One thought on “How to Unzip an Uploaded File with PHP

  1. Pingback: links for 2010-06-04 | Michael Ong | On9 Systems

Comments are closed.