通过PHP的交互式shell命令

问题描述:

我在/ var/www文件夹中有以下index.php文件。通过PHP的交互式shell命令

<html> 
<?php 
$output = shell_exec("/home/ak/Documents/Translation/mosesdecoder/bin/moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt"); 
//$output = shell_exec("cat /home/ak/Documents/Translation/instructions.txt"); 
echo $output; 
?> 
</html> 

的代码试图运行moses程序从input.txt读取输入和输出写入output.txt。我在我的Ubuntu终端上运行了上面编写的moses命令,它工作正常。

但是,当我通过浏览器运行index.php时,即使经过很长时间,output.txt仍保持为空。

注意:我运行了chmod -R 775 /home/ak/Documents/Translation/,因此我可以访问和执行该目录中的文件。作为一项测试,我对注释行进行了取消注释(第shell_execindex.php),该行旨在打印放置在同一文件夹中的instructions.txt的内容。然后,通过浏览器运行index.php,在浏览器上打印instructions.txt的内容。

任何想法,为什么第一个shell_exec通过浏览器运行时失败?

+0

尝试从终端运行此 须藤-u www数据/家/ AK /文档/翻译/ mosesdecoder /斌/摩西-f /家庭/ AK/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt> /home/ak/Documents/Translation/output.txt 然后检查output.txt。如果它为空 - 您必须检查www-data用户的权限。 –

+0

output.txt得到填充运行您建议的命令...所以我认为没有问题的权限 – avinash

加入这一行/home/ak/.bashrc

export PATH=$PATH:/home/ak/Documents/Translation/mosesdecoder/bin 

打开一个新的终端输入moses,以测试它是否工作。现在

更换

$输出=了shell_exec(“/家/ AK /文档/翻译/ mosesdecoder /斌/摩西-f /家庭/ AK /文档/翻译/ ourOwnHinNorm /工作/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt> /home/ak/Documents/Translation/output.txt“);

有:

$output = shell_exec("moses -f /home/ak/Documents/Translation/ourOwnHinNorm/working/binarisedModel/moses.ini -input-file /home/ak/Documents/Translation/input.txt > /home/ak/Documents/Translation/output.txt"); 
+0

它也没有产生任何输出...并且我也提供完整路径的可执行文件在shell命令,所以我不认为只是将其添加到环境路径变量将有所作为......但无论如何 – avinash