2016-08-23 14 views
-2

Ich bin ein Python-Skript über meine PHP laufen. Es funktioniert gut auf der Konsole, aber gibt null zurück, wenn ich es auf dem Browser ausführe. Ich habe sogar versucht, Ausgabe in eine Datei zu schreiben, aber es gibt nichts zurück. Der PHP-Code ist:Führen Sie Skripte in 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

Funktioniert es, wenn Sie den vollständigen Pfad zu Ihrem Python-Skript in '$ y' angeben? –

Antwort

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

Es ist die erste Zeile. Es führt das Skript aus, wenn ich es direkt als "php test.php" auf meinem Terminal ausführe. – yyy

+0

Haben Sie escashellcmd versucht? – Xiaoerge