我有程式路徑為:/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);
運行達不到效果,請教大家有什麼問題嗎?非常感謝
回複討論(解決方案)
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" );
吧
你這樣做下測試呢
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";}