Home > Backend Development > PHP Tutorial > php中popen的使用问题

php中popen的使用问题

WBOY
Release: 2016-06-23 14:15:00
Original
1501 people have browsed it

我有程序路径为:/usr/src/myappMamou/apps/myapp
在命令行中,首先启动程序myapp后,可以输入命令运行程序如:send arg1 arg2 arg3 
这样可以程序可以运行成功

现在想在PHP中完成上述功能,不需要每次在命令行中输入命令 :send arg1 arg2 arg3 也能达到一样的效果

PHP如下:

        $fixedcmd="send 123456063 10000 1111111";        $ph="/usr/src/myappMamou/apps/myapp";        $rs = popen(\"$ph\" \"$fixedcmd\", "r" );	pclose($rs);
Copy after login

运行达不到效果,请教大家有什么问题吗?非常感谢


回复讨论(解决方案)

1、php是通过命令行还是cgi执行,是否执行/usr/src/myappMamou/apps/myapp的权限
2、 “首先启动程序myapp后”是说这个程序单独启动么?能否给出一个send arg1 arg2 arg3 的关系的截图
3、执行启动myapp时当前路径是否必须在一定的位置下?

1、php是通过命令行还是cgi执行,是否执行/usr/src/myappMamou/apps/myapp的权限
2、 “首先启动程序myapp后”是说这个程序单独启动么?能否给出一个send arg1 arg2 arg3 的关系的截图
3、执行启动myapp时当前路径是否必须在一定的位置下?

1、php是放在apache中执行的,具有权限
2、“首先启动程序myapp后”指的是在命令行中启动程序,如./myapp,之后进入程序中myapp>
输入的命令如send arg1 arg2 arg3,send是命令其它三个是参数
3、执行启动myapp时当前路径是否必须在一定的位置下,只有到了/usr/src/myappMamou/apps下才可运行,是不是要弄成系统命令那样才行,就是在任意位置输入命令myapp即可启动程序
请教了

如果我想在任意地方可以执行命令myapp,是不是只要在$PATH中加入myapp的路径就可以了,这样系统就知道myapp的路径,应该就可以识别了吧,但是我试了,无效

$rs = popen(\"$ph\" \"$fixedcmd\", "r" ); ???
咋么也得写作
$rs = popen("$ph $fixedcmd", "r" );

你这样做下测试呢

<?php$descriptorspec = array(   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to);$cwd = '/usr/src/myappMamou/apps';$process = proc_open('myapps', $descriptorspec, $pipes, $cwd);if (is_resource($process)) {    // $pipes now looks like this:    // 0 => writeable handle connected to child stdin    // 1 => readable handle connected to child stdout    // Any error output will be appended to /tmp/error-output.txt	$fixedcmd="send 123456063 10000 1111111";    fwrite($pipes[0], $fixedcmd);    fclose($pipes[0]);    echo stream_get_contents($pipes[1]);    fclose($pipes[1]);    // It is important that you close any pipes before calling    // proc_close in order to avoid a deadlock    $return_value = proc_close($process);    echo "command returned $return_value\n";}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template