Linux shell_exec() 在 "ls" 上返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18241305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:33:01  来源:igfitidea点击:

shell_exec() returning null on "ls"

phplinuxcentos

提问by Mokmeuh

So I have this code and I'm only trying to make a list of the saves in another directory where the php scrip is in xampp folder and the saves are to this path /root/files/saves:

所以我有这个代码,我只是想在另一个目录中制作一个保存列表,其中 php 脚本位于 xampp 文件夹中,保存到这个路径/root/files/saves

<html>
<body>
<?php
$output = shell_exec('ls /root/files/saves');
echo "<pre>$output</pre>";
?>
</body>
</html>

I don't know why I can't get it working on a var_dumpit seems output is null I'm really confuse it should work or I just it all wrong I need some help.

我不知道为什么我不能让它在var_dump似乎输出为空的情况下工作我真的很困惑它应该工作还是我只是错了我需要一些帮助。

采纳答案by Amal Murali

Add 2>&1to the end of your shell command to have STDERRreturned as well as STDOUT.

添加2>&1到 shell 命令的末尾以STDERR返回以及STDOUT.

$output = shell_exec("ls /root/files/saves 2>&1");

Also, if the user running PHP don't have the sufficient permissions to view the output in /root/, then the above code will return a Permission deniederror message.

此外,如果运行 PHP 的用户没有足够的权限查看 中的输出/root/,则上述代码将返回Permission denied错误消息。

Source: http://php.net/manual/en/function.shell-exec.php#28994

来源:http: //php.net/manual/en/function.shell-exec.php#28994