首页 > LAMP/NAMP > proc_open使用示例

proc_open使用示例

2009年5月26日 发表评论 阅读评论

proc_open可以交互执行Linux命令,这边给出一个示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
$command="pwd\n";
 
$descriptorspec = array(
    0 => array('pipe', 'r'),
    1 => array('pipe', 'w'),
    2 => array('pipe', 'w')
);
 
$resource = proc_open($command, $descriptorspec, $pipes, null, $_ENV);
if (is_resource($resource))
{
    fwrite($pipes[0], "pwd\n");
    $stdin = $pipes[0];
    $stdout = $pipes[1];
    $stderr = $pipes[2];
 
    while (! feof($stdout))
    {
        $retval .= fgets($stdout,1024);
    }
 
    while (! feof($stderr))
    {
        $error .= fgets($stderr);
    }
    fwrite($pipes[0], "pwd\n");
    $stdout = $pipes[1];
    $stderr = $pipes[2];
 
    while (! feof($stdout))
    {
        $retval .= fgets($stdout,1024);
    }
 
    while (! feof($stderr))
    {
        $error .= fgets($stderr);
    }
 
    fclose($stdin);
    fclose($stdout);
    fclose($stderr);
 
    $exit_code = proc_close($resource);
}
if (! empty($error))
    throw new Exception($error);
else
    echo $retval;
?>
分类: LAMP/NAMP 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.