If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found.
passthru
(PHP 4, PHP 5)
passthru — Execute an external program and display raw output
설명
The passthru() function is similar to the exec() function in that it executes a command . This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser. A common use for this is to execute something like the pbmplus utilities that can output an image stream directly. By setting the Content-type to image/gif and then calling a pbmplus program to output a gif, you can create PHP scripts that output images directly.
인수
- command
-
The command that will be executed.
- return_var
-
If the return_var argument is present, the return status of the Unix command will be placed here.
반환값
값을 반환하지 않습니다.
주의
사용자가 입력한 데이터를 이 함수로 넘길 때는, escapeshellarg()나 escapeshellcmd()를 이용하여, 사용자가 위험한 명령을 실행하는 조작을 하지 못하게 해야 합니다.
Note: 백그라운드에서 실행하기 위해서 이 함수로 프로그램을 시작하면, 프로그램의 출력은 파일이나, 다른 출력 스트림으로 보내야 합니다. 그렇지 않으면, 프로그램이 종료할 때까지 PHP가 멈출 수 있습니다.
Note: 안전 모드에서 실행 명령은 safe_mode_exec_dir 안에서만 실행할 수 있습니다. 실용적인 이유로, 현재는 실행 경로에 ..을 허용하지 않습니다.
안전 모드에서 명령어는 escapeshellcmd()로 회피처리됩니다. 그러므로, echo y | echo x는 echo y \| echo x가 됩니다.
참고
- exec() - Execute an external program
- system() - Execute an external program and display the output
- popen() - Opens process file pointer
- escapeshellcmd() - Escape shell metacharacters
- backtick operator
passthru
30-Jul-2008 01:48
23-Nov-2007 05:17
Note to Paul Giblock: the command *is* run through the shell.
You can verify this on any Linux system with
<?php
passthru ('echo $PATH');
?>
You'll get the content of the PATH environment variable, not the string $PATH.
19-May-2007 03:30
Stuart:
The pasthru function does not execute the program through the shell. What this mean, among other things, is that your PATH variable is never set. Therefore, you have to use full paths on everything.
I believe system() will run your program underneith a shell. This allow the program to run in a 'normal' environment.
-Paul
03-Jan-2006 10:51
I wrote function, that gets proxy server value from the Internet Explorer (from
registry). It was tested in Windows XP Pro
(Sorry for my English)
<?php
function getProxyFromIE()
{
exec("reg query \"HKEY_CURRENT_USER\Software\Microsoft".
"\Windows\CurrentVersion\Internet Settings\" /v ProxyEnable",
$proxyenable,$proxyenable_status);
exec("reg query \"HKEY_CURRENT_USER\Software\Microsoft".
"\Windows\CurrentVersion\Internet Settings\" /v ProxyServer",
$proxyserver);
if($proxyenable_status!=0)
return false; #Can't access the registry! Very very bad...
else
{
$enabled=substr($proxyenable[4],-1,1);
if($enabled==0)
return false;
else
{
$proxy=ereg_replace("^[ \t]{1,10}ProxyServer\tREG_SZ[ \t]{1,20}","",
$proxyserver[4]);
if(ereg("[\=\;]",$proxy))
{
$proxy=explode(";",$proxy);
foreach($proxy as $i => $v)
{
if(ereg("http",$v))
{
$proxy=str_replace("http=","",$v);
break;
}
}
if(@!ereg("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:".
"[0-9]{1,5}$",$proxy))
return false;
else
return $proxy;
}
else
return $proxy;
}
}
}
?>
Note, that this function returns FALSE if proxy is disabled in Internet
Explorer. This function returns ONLY HTTP proxy server.
Usage:
<?php
$proxy=getProxyFromIE();
if(!$proxy)
echo "Can't get proxy!";
else
echo $proxy;
?>
09-Dec-2005 04:24
I dunno if anyone else might find this useful, but when I was trying to use the passthru() command on Suse9.3 I was having no success with the command:
$command = 'gdal_translate blahahahaha';
passthru($command);
It only worked once I put:
$command = '/usr/bin/local/gdal_translate blalalala';
passthru($command);
13-Oct-2005 07:09
I had an issue when i used exec
I think we were echoing information on the test.php script.
for eg: when we tried
exec(php test.php,$array,$error);
the return was 127 and the code was failing.
checking the note on this page gave us a hint to use passthru instead.
The only thing to note is that you need to provide the fuull path.
now our command became
passthru(/bin/php /pathtotest/test.php,$array,$error);
this works.
yipeee!!!!!
22-Sep-2005 11:59
When upgrading my redhat server to enterprise 4, selinux was turned on. This caused one of my php scripts (that uses passthru) to fail. After some nice help from redhat, I was able to get the script running again. Here is what helped me.
If you get permission errors (in /var/log/httpd/error_log) which seem to be from selinux (and not standard chmod or chown issues), make sure that the folder you are using is not in /tmp and has the selinux context of httpd_sys_script_rw_t as can be set as follows:
chcon -t httpd_sys_script_rw_t folder_name
Hope this helps someone...
09-Aug-2005 11:52
Thought it might beuseful to note the passthru seems to supress error messages whilst being run in Dos on Windows (test on NT).
To show FULL raw output including errors, use system().
24-Jun-2005 05:33
If you are using passthru() to download files (for dynamically generated content or something outside webserver root) using similar code:
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"myfile.zip\"");
header("Content-Length: 11111");
passthru("cat myfile.zip",$err);
and your download goes fine, but subsequent downloads / link clicks are screwed up, with headers and binary data being all over the website, try putting
exit();
after the passthrough. This will exit the script after the download is done and will not interfere with any future actions.
09-Mar-2005 04:33
Zak Estrada
14-Dec-2004 11:21
Remember to use the full path (IE '/usr/local/bin/foo' instead of 'foo') when using passthru, otherwise you'll get an exit code of 127 (command not found).
Remember, you'll also get this error if your file does not have executable permission.
03-Mar-2005 07:50
Regarding swbrown's comment...you need to use an output buffer if you don't want the data displayed.
For example:
ob_start();
passthru("<i>command</i>");
$var = ob_get_contents();
ob_end_clean(); //Use this instead of ob_flush()
This gets all the output from the command, and exits without sending any data to stdout.
15-Dec-2004 01:21
Remember to use the full path (IE '/usr/local/bin/foo' instead of 'foo') when using passthru, otherwise you'll get an exit code of 127 (command not found).
28-May-2004 12:30
Regarding kpierre's post, be mindful that if you shell script errors, you will find the error output from it in the base error_log file (not virtualhost error_log) in apache.
05-Sep-2003 03:23
With apache 2.x on RH9 passthru() writes 1 byte at a time. Apache 2.x buffers and chunk encodes the output for you - but the chunked encoding devides the output in chunks of 1 byte each...thus several bytes of overhead per byte. I guess that buffering behaviour is by design - but caused problems for me with IE adobe acrobot 5 plugin. The plugin doesn't like like it if you send it a stream of 1 byte chunks - it tells you your file is not a pdf or gives a blank screen. Using output buffering (ob_start / ob_endflush) gives reasonable size chunks and the plugin works OK.
04-Jun-2003 12:41
passthru() seems absolutely determined to buffer output no matter what you do, even with ob_implicit_flush(). The solution seems to be to use popen() instead.
31-Jan-2002 12:35
The documention does not mention that passthru() will only display standard output and not standard error.
If you are running a script you can pipe the STDERR to STDOUT by doing
exec 2>&1
Eg. the script below will actually print something with the passthru() function...
#!/bin/sh
exec 2>&1
ulimit -t 60
cat nosuchfile.txt
03-Oct-2001 11:51
If you sometimes get no output from passthru() use system() instead. This solved this problem for me (php 4.0.5 on Tru64 Unix compiled with gcc).
21-Jun-2001 09:25
PJ's ulimit example is nice; however, if you include multiple commands in the script after the ulimit command, each gets its own, seperate 60 second time slot!<br>
Furthermore, these sixty seconds are *CPU* time. Most programs hang for other reasons than CPU hogging (for example, waiting for a database connection) so for most purposes the number 60 is rather too high.<br>
Try "ulimit -t 1" first, which will give you about 10^9 cycles on modern hardware -- quite enough to get a lot of work done!
15-Feb-2001 10:06
About the problem of zombies, you may call a bash script like this:
--------------------------
#! /bin/bash
ulimit -t 60
<your command here>
--------------------------
