2016-08-23 209 views
-2

我正在通过php运行python脚本。它在控制台上工作正常,但在浏览器上运行时返回null。我甚至尝试将输出写入文件,但它不返回任何内容。 PHP代码为:在php中运行脚本

<?php 

$y = "python Code.py"; 

$output = shell_exec($y); 

if($output!=null){ 
echo $output; 
} 
else { 
echo "No output"; 
} 




$myfile = fopen("test.txt", "w") or die("Unable to open file!"); 

fwrite($myfile, $output); 

fclose($myfile); 


?> 
+0

请问,如果你指定要在'$ y'你的Python脚本的完整路径它的工作? –

回答

0
$command = escapeshellcmd('/usr/custom/test.py'); 
$output = shell_exec($command); 
echo $output; 

#!/usr/bin/env python **is this in the first line of you python script?** 
chmod +x /usr/custom/test.py 
+0

这是第一行。如果我在终端上直接以“php test.php”的形式运行它,它会执行脚本。 – yyy

+0

你试过escapeshellcmd吗? – Xiaoerge