简体   繁体   English

PHP:从PHP运行shell命令?

[英]PHP: Running a shell command from PHP?

echo shell_exec("ll");

为什么运行此代码时php为什么不显示文件和目录列表?

You're using an aliased command. 您正在使用别名命令。 That's why. 这就是为什么。 What happens when you run ls ? 运行ls会发生什么?

Source: PHP page for shell_exec and experience 来源: 用于shell_exec和经验的PHP页面

ll isn't the command. ll不是命令。 I believe that echo shell_exec("ls"); 我相信echo shell_exec("ls"); will output a list of directories. 将输出目录列表。

Also you can have some problems if you are running PHP in safe mode. 如果以安全模式运行PHP,也会遇到一些问题。 Here you have more information: http://php.net/manual/en/function.shell-exec.php 在这里,您可以获得更多信息: http : //php.net/manual/en/function.shell-exec.php

shell_exec : This function is disabled when PHP is running in safe mode. shell_exec :当PHP在安全模式下运行时,此功能被禁用。

Eventually, you have more secure alternatives to execute a command in a bash like readdir : http://php.net/manual/en/function.readdir.php . 最后,你有更多安全的替代像一个bash执行命令readdirhttp://php.net/manual/en/function.readdir.php This is a function that read the files that are in a certain directory. 此功能可读取特定目录中的文件。

readdir($dir_handle) example: readdir($dir_handle)示例:

<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

    closedir($handle);
}
?>

ll is often added as a default alias in bash (in your .bashrc file). ll通常作为默认别名添加到bash中(在.bashrc文件中)。

To double check whether ot not this is the case, type the following in your terminal: 要再次检查是否存在这种情况,请在终端中键入以下内容:

type -a ll

Your shell will tell you whether or not ll is an alias or an actual program. 您的shell会告诉您ll是别名还是实际程序。

PHP runs in a different environment which does not have your default aliases installed. PHP在未安装默认别名的环境中运行。

The command expression you probably want is: 您可能需要的命令表达式是:

shell_exec("ls -l");

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

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