passthru

(PHP 4, PHP 5, PHP 7, PHP 8)

passthruExecuta um programa externo e mostra a saída bruta

Descrição

passthru(string $command, int &$result_code = null): ?false

A função passthru() é similar à função exec(), e executa um comando especificado em command. Esta função deve ser usada no lugar de exec() ou system() quando a saída do comando Unix for dado binário que precisa ser passado diretamente de volta ao navegador. Um uso como para isto é executar algo como os utilitários pbmplus que podem enviar um fluxo de imagem diretamente. Através da configuração de Content-type para image/gif e da chamada ao programa pbmplus para saída de um arquivo gif, pode-se criar scripts PHP que mostram imagens diretamente.

Parâmetros

command

O comando que será executado.

result_code

Se o argumento result_code estiver presente, o estado de retorno do comando Unix será colocado aqui.

Valor Retornado

Retorna null em caso de sucesso ou false em caso de falha.

Erros/Exceções

Emitirá um E_WARNING se passthru() for incapaz de executar o comando especificado no parâmetro command.

Lança uma exceção ValueError se command estiver vazio ou se contiver bytes nulos.

Registro de Alterações

Versão Descrição
8.0.0 Se command estiver vazio ou contiver bytes nulos, passthru() agora lança uma exceção ValueError. Anteriormente, emitia um E_WARNING e retornava false.

Notas

Aviso

Ao permitir que os dados fornecidos pelo usuário sejam passados para esta função, use escapeshellarg() ou escapeshellcmd() para garantir que os usuários não possam induzir o sistema a executar comandos arbitrários.

Nota:

Se um programa for iniciado com esta função, para que continue rodando em segundo plano, a saída do programa deve ser redirecionada para um arquivo ou outro stream de saída. Deixar de fazer isso fará com que o PHP trave até que a execução do programa termine.

Veja Também

add a note add a note

User Contributed Notes 21 notes

up
18
puppy at cyberpuppy dot org
19 years ago
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.
up
6
jo at durchholz dot org
16 years ago
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.
up
8
igor at bboy dot ru
18 years ago
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.
up
4
swbrown at ucsd dot edu
20 years ago
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.
up
3
kpierre at fit dot edu
22 years ago
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
up
1
nuker at list dot ru
18 years ago
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;
?>
up
1
Zak Estrada
19 years ago
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).
up
1
Chroot
15 years ago
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.
up
0
myselfasunder at gmail dot com dot dfvuks
13 years ago
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
up
-1
tox at novasonica dot com
4 years ago
I was trying to implement a system that allows running arbitrary CLI commands with parameters, but I kept running into the issues with user prompts from the command as they would let execution hang. The solution is simple: just use passthru() as it outputs everything and correctly handles user prompts out of the box.
up
0
Paul Giblock
16 years ago
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
up
0
Stuart Eve
18 years ago
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);
up
0
stuartc1 at NOSPAM dot hotmail dot com
18 years ago
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().
up
0
sarel dot w at envent dot co dot za
19 years ago
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.
up
-1
sidney at jigsaw dot nl
22 years ago
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!
up
-1
PJ at piggei dot com
23 years ago
About the problem of zombies, you may call a bash script like this:

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

<your command here>
--------------------------
up
-2
vijayramanan at rediffmail dot com
18 years ago
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!!!!!
up
-5
me at yourMomsPussy dot cum
7 years ago
`command` // back ticks drop you out of PHP mode into shell
exec('command', $output); // exec will allow you to capture the return of a command as reference
shell_exec('command'); // will return the output to a variable
system(); //as seen above.
up
-5
andreas dot hochsteger at oeamtc dot at
22 years ago
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).
up
-4
php @ richud dot com
19 years ago
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.
up
-6
jcr at marvel-databadge dot com
20 years ago
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.
To Top