Archive

Posts Tagged ‘unzip php’

How to Determine Path to unzip With PHP

September 5th, 2010 No comments

Sometimes you need to know paths to some system software that needs to be executed with your script. For example, in order to unzip a file without installing or using any specific PHP classes or extensions, you need to know where the unzip binary is located.

The solution is very simple. The only restriction is that you should have rights to use exec() or system(). Though, I don’t think there is any reason to deal with the paths if you are not allowed to run system applications. Here is the code:


$var=exec ("which unzip");
echo $var;

What does this code do? It catches the output of system which command to a variable. Then you can use this variable for your purposes. Of course, you should verify the value of the variable before using it. You may also need to echo something, if unzip is not installed.

This technology can be applied to any other system binary you need to know the path of. $var=exec (“which sh”); will tell you the location of sh, if you need to run any shell scripts with your PHP script, and so on.

How to Unzip an Uploaded File with PHP

November 17th, 2008 No comments

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']))
{
$fishierul=”data.zip”;
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 data.zip. 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.