How to Determine Path to unzip With PHP

By | September 5, 2010

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.