Re: checking for utilities/applications in the PATH
| From: | Michal Migurski | Date: | Thu, 26 Aug 2004 00:51:27 +0000 |
| Subject: | Re: checking for utilities/applications in the PATH | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194744@lists.php.net to get a copy of this message | ||
> i am trying to do a simple check for archiving utilities (zip, unzip,
> ...), to make sure they exist within the PATH, and are executable by
> PHP.
>
> i could not find a better way to do this through php, so i went ahead
> and did an
>
> exec('which ' . $utility, $output, $return_value);
>
> then, i check the $return_value for a non-zero value, which would
> indicate that the utility was not found. using GNU/Linux this seems to
> work fine, but i wouldn't mind keeping my work cross-platform.
>
> so, my question is, is there a better way to do this?
If you don't want to use the shell, you can check the $_SERVER['PATH']
environment variable. On my (linux) system it looks like
"sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin", so you could explode() that
on ':' and then cycle through the files in each directory using readdir(),
checking each with is_executable().
You could also just check the output of your exec() call - it should be
the full path of the executable you're looking for. An empty return would
imply that there isn't a matching executable. I think which always returns
just the first match, since it's meant to show what would get called if
you were to use that command.
Personally I love using exec() to make php behave like bash at times, but
posts on this list from pparently knowledgeable people occasionally imply
that it's not the best/fastest/most stable method to use, so I try not to.
-mike.
---------------------------------------------------------------------
michal migurski- contact info and pgp key:
sf/ca http://mike.teczno.com/contact.html