简体   繁体   English

exec()和passthru()php执行

[英]exec() and passthru() php executing

I try to execute a python script with PHP But I got no results:I tried 我尝试使用PHP执行python脚本,但没有结果:我尝试过

$tmp = passthru("C:\\Python27\\python.exe C:\\Python27\\script.py C:\\Python27\\file.pdf",$output);
print($output)

The results :"1" 结果:“ 1”

While

$tmp = exec("C:\\Python27\\python.exe C:\\Python27\\script.py C:\\Python27\\file.pdf",$output);

returns Array ( ) 返回数组()

I'm excepted to return a string,any suggestions? 我不能返回字符串,有什么建议吗? I verified in my php.ini file 我在php.ini文件中验证了

  safe_mode = Off

Thanks!! 谢谢!!

The documentation of exec() says that the $output parameter is an array that will be filled in with each line. exec()的文档说$output参数是一个数组,将在每一行中填充。 If you want to turn this into a single string, use: 如果要将其转换为单个字符串,请使用:

$output_string = implode("\n", $output);

This should work for you: 这应该为您工作:

ob_start();

passthru("C:\\Python27\\python.exe C:\\Python27\\script.py C:\\Python27\\file.pdf");

$filedata = ob_get_clean();

echo $filedata;

python buffers output by default. python默认情况下缓冲输出。 Set environment variable PYTHONUNBUFFERED to a nonempty string, or run python with -u. 将环境变量PYTHONUNBUFFERED设置为非空字符串,或使用-u运行python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM