2016-08-23 199 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