★ wanayoo — archive 1999 http://www.php.net/manual/function.exec.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Program Execution
Quick Reference
Program Execution
* escapeshellcmd
* exec
* passthru
* system
Manual: exec
View the source code for this pageSearch the site



Previous page
 escapeshellcmd
passthru 
Next page


exec

(PHP3 , PHP4 )

exec -- Execute an external program

Description

string exec (string command [, string array [, int return_var]])

exec() executes the given command, however it does not output anything. It simply returns the last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the PassThru() function.

If the array argument is present, then the specified array will be filled with every line of output from the command. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

If the return_var argument is present along with the array argument, then the return status of the executed command will be written to this variable.

Note that if you are going to allow data coming from user input to be passed to this function, then you should be using EscapeShellCmd() to make sure that users cannot trick the system into executing arbitrary commands.

See also system(), PassThru(), popen(), EscapeShellCmd(), and the backtick operator.


User Contributed Notes: exec


landon@bradshaw.org
28-Jul-1999 11:30
<p><i>STDERR</i> is not handled and any output to will be passed to the browser as part of the page.</p> <p>To stop this from happening you can append '2>&1' on the end of your command-line to redirect STDERR to STDOUT.</p>


dpk@nwserv.com
26-Aug-1999 09:49
Note that for some reason PHP does not pass the environment on to the called programs.


markmadsen@yahoo.com
27-Dec-1999 11:38
PHP 3.0.12 is required for this to work on Windows 2000 (and possibly Winnt 4 but I can't confirm that). PHP 3.0.12 is not available as a binary, you must compile it.


eric@interplas.com
17-Feb-2000 04:01
You may find running /usr/bin programs will not work because the 'nobody' user environment is somewhat skimpy. putenv("HOME=/home/nobody") and putenv("USER=nobody") before the exec() call will help (as long as you've edited /etc/passwd so that 'nobody' can have a place to call home).


bert@box.balpol.tudelft.nl
29-Feb-2000 01:34
After replacing PHP4b3 with PHP4b4pl1, exec("some/unix/command") silently didn't return anything anymore. It turned out that exec() seems to be changed into shell_exec(). At least for me everything worked after replacing exec() with shell_exec().


geek@freemail.it
02-Mar-2000 04:37
i need to print in my php page the output of a script which is http located, i have noticed that exec works just for local scripts.. in this case i have a C script which generate me some html code (banner image and its href) and a php where i have to inert that code here and there... please help, thanks


tspaeth@netactive.de
10-Mar-2000 04:32
I agree to bert@box.balpol.tudelft.nl with that "exec" stuff. But "shell_exec" seems undocumented!?!


gysi@cyberlink.ch
11-Mar-2000 05:37
with the upgrade to php 3.0.15 the exec/passthru functions doesn't return <string>. Example:
&lt;? 
$pwd = exec("pwd");
echo $pwd;
?>
will print nothing, so $pwd is empty. However, when I use system() instead of exec() there is an output from the system()-function itself, but $pwd is also empty this time. The output from the system()-function proves that the command was succsessfuly executed, but the value wasn't submitet to $pwd. Has anybody else got this misbehavior? Does anybody have a quick fix for this?


keith@MagicClicks.com
15-Mar-2000 01:08
If PHP is waiting on your *NIX background processes, try this redirecting all output to /dev/null: <br>
nohup perl mycmd.pl &gt;/dev/null 2&gt;&amp;1 &amp;
<br> I didn't need the output. If you do...???


keith@MagicClicks.com
15-Mar-2000 01:10
DOH!!!
nohup perl mycmd.pl >/dev/null 2>&1 &



pierre-henri@wanadoo.be
21-Mar-2000 09:41
Exec method and release 3.0.15. As describe before the Exec methods doesn't return any output anymore. You can get the result of the command using the second parameter on the function wich is an array.


pm@rednet.com.ar
22-Mar-2000 06:55
I need to execute a perl script from inside a php3, how can I do this ? I have a redir.pl to redirect pages, but I try with the examples here and I can't make it work What I'm missing ? Pablo Murillo


zwerschke@urz.uni-heidelberg.de
24-Mar-2000 02:20
exec() does not return the last line of the output in PHP 3.0.15. This is a bug and surely will be fixed in the next release. See also http://bugs.php.net/bugs.php3?id=3915


j_herlit@informatik.uni-kl.de
28-Mar-2000 09:41
exec("/usr/bin/touch /tmp/testfile");
doesn´t do anything. Neither system() nor passthru() does something. What am I doing wrong ?


giwarden@ucla.edu
01-Apr-2000 04:22
I'm having trouble getting php to fire off a background process and proceed asynchronously without caring about the status of that other process. I got it to *sort of* work by using the above suggesting using nohup or by using a perl script as a wrapper to close the STDIN, STDOUT, and STDERR, and fork the process. The reason it only sort of works is because the rest of the php code on that transaction gets processed, but any further requests with that browser just hang (it's okay if I close netscape and connect again though). Does this have anything to do with sessions? Any ideas for forking asynchronous background processes with php, or do I have to come up with something else to do this?


cregnier@etu.info.unicaen.fr
04-Apr-2000 06:53
I wonder how to create a directory $ref=shell_exec("mkdir ../aa";); but it doesn't works. thanks


james@whispering.org
05-Apr-2000 04:12
Is it possible to pass PHP3 variables as arguments to the system() or exec() command lines? Whenever I try to pass one with something such as system("/bin/blah $var1 $var2"), the PHP preprocessor tells me: "Warning: Cannot pass expression as argument 2 by reference in ..."


ppruett@spacecoast.org
06-Apr-2000 02:06
I wanted to pass a lot of variables from a php shopping cart, MyCart, to the c program that does my credit card processing, www.cardservice.com/linkpt I wonder how to best pass the environment on to the called programs? any thoughts? For example I could have php save to sql database then pass the key to my program which would have to be written to know who to query the the sql server, pain... I could make a big ugly array in put in the back tick... oh well...


verolix@email.com
24-Apr-2000 07:28
I want to execute 'ipchains' inside php3 script.. I am quite sure, I have seen sombody does this.. but how?.. it said permission denied all the time? I am running Apache with php3 as a module


brads@avalon.net
25-Apr-2000 01:44
I have been able to pass $variables in both system and exec but I need to call a program that requires double quotes around the input : system("/programname "$variable1" $variable2"); So does anyone know what the escape character is for " quotes" within a system() call???


nv@mindimage.net
25-Apr-2000 08:55
brads: try it with $program = "/program \"" . $var1 . "\" " . "\"" . $var2 . "\""; system($program); not sure if it works though


brads@avalon.net
03-May-2000 03:26
I ended up setting a variable $quote='"' thats a double quote and it works so smooth...


compwiz@bigfoot.com
05-May-2000 09:30
Is there any mechanism is there to stop a user from doing exec("/bin/rm /some/file/owned/by/nobody"); ? When I tried such a command, to my surprise, it actually worked.


terry@terrym.com
09-May-2000 05:49
The above "See Also" reference to "EscapeShellCmd" should be linked to:
http://www.php.net/manual/function.escapeshellcmd.php
One other also needs linked.

Terry


olive@deep-ocean.net
10-May-2000 01:35
hello all. i'm wondering how to execute commands as another user. that is, i protect a page with a login/pass. i get the login/pass ($PHP_AUTH_*), and now, i would like to do:
    exec($PHP_AUTH_USER,$PHP_AUTH_PASS,"$myCommand",$output,$return);
i've tried
exec("echo $PASSWD | su $USER -c $command");
but su doesn't accept this behavior. any help would be appreciated...


nagey@ccweb.com
17-May-2000 05:39
is there a good reason why i cant get the vars $username and $passwd from a form and then do:
exec("/usr/local/bin/htpasswd -b /web/whatever/.htpasswd ".$username." ".$passwd);
it doesnt work with shell or passthru either. why?


 About Notes


Previous page
 escapeshellcmd
passthru 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.