PHP: system() vs exec() vs shell_exec() vs passthru()
An Example of simple C Code (a.out)
代碼:
<?PHP
$cmd = "a.out";
echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
?>
代碼:
$ cat stdout-stderr-fprintf.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
fprintf(stderr, "hello\n");
fprintf(stderr, "thank\n");
fprintf(stdout, "you\n");
fprintf(stdout, "welcome\n");
fprintf(stderr, "to\n");
fprintf(stderr, "my home\n");
return -15;
}
$ gcc stdout-stderr-fprintf.c
$ ls a.out
a.out
PHP: System(): console=STDOUT+STDERR, return last line of STDOUT
代碼:
------------------------------------------------
hello
thank
to
my home
you
welcome
------------------------------------------------
string(7) "welcome"
================================================
PHP: exec(): console=STDERR only, return last line of STDOUT
代碼:
------------------------------------------------
hello
thank
to
my home
------------------------------------------------
string(7) "welcome"
================================================
PHP: shell_exec(): console=STDERR only, return all lines of STDOUT
代碼:
------------------------------------------------
hello
thank
to
my home
------------------------------------------------
string(12) "you
welcome
"
================================================
PHP: passthru(): console=STDOUT+STDERR, return NULL
代碼:
------------------------------------------------
hello
thank
to
my home
you
welcome
------------------------------------------------
NULL
================================================
Example: "head -3 /proc/cpuinfo"
代碼:
<?PHP
$cmd = "head -3 /proc/cpuinfo";
echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";
?>
@@@ RESULT @@@
------------------------------------------------
processor : 0
vendor_id : GenuineIntel
------------------------------------------------
string(24) "vendor_id : GenuineIntel"
================================================
------------------------------------------------
------------------------------------------------
string(24) "vendor_id : GenuineIntel"
================================================
------------------------------------------------
------------------------------------------------
string(39) "processor : 0
vendor_id : GenuineIntel
"
================================================
------------------------------------------------
processor : 0
vendor_id : GenuineIntel
------------------------------------------------
NULL
================================================
________________
美術插畫設計案子報價系統 v0.1 Beta
爪哇禾雀
|