我有这个PHP代码,我想在网页上打印结果,没有任何内容出现在它但是当我在命令行中运行它时一切正常
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$str =exec("python mainsummy.py",$output,$return_var);
#return_var int
#output array
var_dump ($output);
print_r($output);
echo "the 4 element \n";
echo $output[4];
print $output[4];
?>
</body>
</html>
这是网页上显示的内容:
array(0) { } Array ( ) the 4 element
以下是CLI中显示的内容:
<html><head><title>hello</title></head><body>array(5) { [0]=> string(269) "De jure (/dᵻ ˈdʒʊəriː/, /deɪ-/; Classical Latin: de iure [deː ˈjuːrɛ]) is an expression that means \"of right, by right, according to law\" (literally \"from law\"), as contrasted with de facto, which means \"in fact, in reality\" (literally \"from fact\")." [1]=> string(150) "The terms de jure and de facto are used instead of \"in law\" and \"in practice\", respectively, when one is describing political or legal situations." [2]=> string(178) "In a legal context, de jure is contrasted to de facto practices, where, for example, the people obey a contract as though there were a law enforcing it, yet there is no such law." [3]=> string(95) "Ahmad, however, was in practice (de facto) the actual Sultan, and his brother was a figurehead." [4]=> string(117) "Thus, Egypt was by Ottoman law de jure a province of the Ottoman Empire, but de facto was part of the British Empire."}Array( [0] => De jure (/dᵻ ˈdʒʊəriː/, /deɪ-/; Classical Latin: de iure [deː ˈjuːrɛ]) is an expression that means \"of right, by right, according to law\" (literally \"from law\"), as contrasted with de facto, which means \"in fact, in reality\" (literally \"from fact\"). [1] => The terms de jure and de facto are used instead of \"in law\" and \"in practice\", respectively, when one is describing political or legal situations. [2] => In a legal context, de jure is contrasted to de facto practices, where, for example, the people obey a contract as though there were a law enforcing it, yet there is no such law. [3] => Ahmad, however, was in practice (de facto) the actual Sultan, and his brother was a figurehead. [4] => Thus, Egypt was by Ottoman law de jure a province of the Ottoman Empire, but de facto was part of the British Empire.)the 4 element Thus, Egypt was by Ottoman law de jure a province of the Ottoman Empire, but de facto was part of the British Empire.Thus, Egypt was by Ottoman law de jure a province of the Ottoman Empire, but de facto was part of the British Empire.</body></html>
答案 0 :(得分:0)
您应该更新您的exec以确保在运行命令之前以特定用户的身份静默登录,因为这可能是权限问题。
例如(假设你在Linux上)如果你想确保它是由用户运行的那么&#34; root&#34;有权限......
exec("sudo -u root -S python mainsummy.py",etc,etc);
也可以是&#34; mainsummy.py&#34;的位置。脚本。在PHP中,您假设它知道文件实际存在的位置。尝试使用它的完整路径。
exec("python '/var/full/path/to/file/mainsummy.py'",etc,etc);
答案 1 :(得分:0)
问题是权限服务器端: 在/ etc / sudoers
中添加www-data Apache的用户权限时,我解决了我的解决方案1)nano / etc / sudoers
2)最后添加这一行 www-data ALL = NOPASSWD:ALL
3)就是这样,现在使用exec()记得在命令之前使用 sudo 。
exec(“sudo python /FULL_PATH/mainsummy.py”,$ output,$ return_var);