PHP Doku:: Führt ein externes Programm aus und zeigt dessen Ausgabe an - function.passthru.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur ProzesskontrolleAusführung von SystemprogrammenFunktionen zur Programmausführungpassthru

Ein Service von Reinhard Neidl - Webprogrammierung.

Funktionen zur Programmausführung

<<exec

proc_close>>

passthru

(PHP 4, PHP 5)

passthruFührt ein externes Programm aus und zeigt dessen Ausgabe an

Beschreibung

void passthru ( string $command [, int &$return_var ] )

Die Funktion passthru() ähnelt der Funktion exec(), da sie ebenfalls ein command ausführt. Diese Funktion sollte anstelle von exec() bzw. system() verwendet werden, wenn die Ausgabe des Unixkommandos binäre Daten erzeugt, die direkt an den Browser zurückgegeben werden sollen. Ein praktisches Beispiel hierfür ist die Ausführung des pbmplus-Utilities, welches einen Bildstream direkt ausgibt. Wenn Sie den Content-Type auf image/gif setzen und dann das pbmplus-Programm aufrufen, um ein GIF zu erzeugen, können Sie auf diesem Weg mittels PHP direkt ein Bild erzeugen und ausgeben.

Parameter-Liste

command

Das auszuführende Programm.

return_var

Ist der Parameter return_var angegeben, wird der Rückgabestatus des UNIX-Befehls hier abgelegt.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Anmerkungen

Warnung

Falls Sie es erlauben, dass Daten von Usereingaben an diese Funktion weitergereicht werden, sollten Sie escapeshellarg() oder escapeshellcmd() verwenden. Bei Verwendung dieser Funktionen stellen Sie sicher, dass kein Benutzer Ihr System überlisten kann, beliebige Kommandos auszuführen

Hinweis:

Falls Sie diese Funktion benutzen, um ein Programm zu starten und im Hintergrund weiterlaufen lassen möchten, müssen Sie sicherstellen, dass die Ausgabe des Programms in eine Datei oder einen anderen Ausgabestream umgeleitet wird. Anderenfalls wird PHP solange hängen, bis das Programm zu Ende ausgeführt wurde.

Hinweis: Mit aktiviertem Safe Mode können Sie nur Programme ausführen, die im Verzeichnis liegen, das mit safe_mode_exec_dir angegeben wurde. Aus praktischen Gründen ist es gegenwärtig nicht erlaubt, dass der Pfad zu einem Programm .. enthält.

Warnung

Mit aktiviertem Safe Mode wird der Kommandostring mit escapeshellcmd() kodiert. Deshalb wird echo y | echo x zu echo y \| echo x.

Siehe auch


22 BenutzerBeiträge:
- Beiträge aktualisieren...
myselfasunder at gmail dot com dot dfvuks
29.11.2010 22:00
PHP's program-execution commands fail miserably when it comes to STDERR, and the proc_open() command doesn't work all that consistently in non-blocking mode under Windows.

This command, although useful, is no different. To form a mechanism that will see/capture both STDOUT and STDERR output, pipe the command to the 'tee' command (which can be found for Windows), and wrap the whole thing in output buffering.

Dustin Oprea
houston_roadrunner at yahoo dot com
27.10.2010 19:16
It is worth noting that the passthru can be picky about the quotes used...

this does not work.
passthru("'lp -d ".$printer." /var/www/BE2/java2/deliveryticket/".$store."/".$filename."'");

but this does.
passthru("lp -d $printer /var/www/BE2/java2/deliveryticket/$store/$filename");   

tested on debian sarge...
ignacio paz posse
6.04.2010 16:15
I'd like to refer about the use of use of passthru as a substitute to include or require which I didn't see before. It helped me to workaround a problem with a script which fatally failed only when included or required. I presume that it had to be with recursive includes because there where memory exhaustion messages that didn't get fixed even when increasing the memory limit with ini_set().  As I had no time to re-code it, the fix of the problem was to get the output of the script  via a passthru call.

Something like this:
<?php
# ...
# (function stuff)
ob_start();
passthru("php myscript.php, $result");
$content_grabbed=ob_get_contents();
ob_end_clean();

if(
$result=0){
 echo
'<div>',$content_grabbed,'</div>';
}
_
# (more function stuff)
# ...
?>

I don't know about drawbacks that this method might have. Maybe and increased overhead? Anyway I was happy to circumvent the problem with it.
Chroot
29.07.2008 18:48
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.
jo at durchholz dot org
22.11.2007 21: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.
Paul Giblock
18.05.2007 20: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
nuker at list dot ru
3.01.2006 14: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;
?>
Stuart Eve
8.12.2005 20: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);
vijayramanan at rediffmail dot com
13.10.2005 12: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!!!!!
waldow at NOSPAM dot chem dot plu dot edu
22.09.2005 16: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...
stuartc1 at NOSPAM dot hotmail dot com
9.08.2005 16: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().
igor at bboy dot ru
23.06.2005 22: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.
sarel dot w at envent dot co dot za
9.03.2005 8: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.
puppy at cyberpuppy dot org
2.03.2005 23: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.
Zak Estrada
14.12.2004 17: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).
php @ richud dot com
27.05.2004 17: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.
jcr at marvel-databadge dot com
4.09.2003 20: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.
swbrown at ucsd dot edu
4.06.2003 5: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.
kpierre at fit dot edu
30.01.2002 16: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
andreas dot hochsteger at oeamtc dot at
3.10.2001 16: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).
sidney at jigsaw dot nl
21.06.2001 2: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!
PJ at piggei dot com
15.02.2001 2:06
About the problem of zombies, you may call a bash script like this:

--------------------------
#! /bin/bash
ulimit -t 60

<your command here>
--------------------------



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)