简体   繁体   English

使用PHP exec执行命令

[英]Executing commands with PHP exec

I am working with one of my projects. 我正在处理我的一个项目。 I am using XAMPP on my windows machine to develop the project. 我在Windows机器上使用XAMPP开发项目。 Here is the problem I am facing. 这是我面临的问题。 I need to exec a shell script on the server and display the result on a webpage.The problem is that most of the script is functioning as expected but I am not able to get the output of the following commands, 我需要在服务器上执行Shell脚本并在网页上显示结果。问题是大多数脚本都按预期运行,但是我无法获得以下命令的输出,

ls, cat, pwd ls,猫,pwd

as these commands return me a blank array. 这些命令将返回一个空白数组。

I am not able to find the exact problem. 我找不到确切的问题。

This isn't a direct answer to your question, but I felt it was important to point out that the functionality of ls , cat and pwd can be simulated within PHP, so if all you're doing is calling those commands and passing the results back into PHP, then there might not be any point in calling them at all. 这不是您问题的直接答案,但我认为必须指出,可以在PHP中模拟lscatpwd的功能,因此,如果您要做的就是调用这些命令并传递结果,则非常重要回到PHP,那么调用它们可能根本没有意义。

ls is a directory listing. ls是目录列表。 PHP provides a class called DirectoryIterator that can do exactly this in just a few lines of code: PHP提供了一个名为DirectoryIterator的类,该类只需几行代码即可完成此操作:

An example: 一个例子:

$iterator = new DirectoryIterator($directory);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        print "File: ".$fileinfo->getFilename();
    }
}

pwd gets the current directory. pwd获取当前目录。 PHP has a built-in function getcwd() that does this in a single line of code. PHP具有内置函数getcwd() ,可在一行代码中完成此操作。

cat prints files (and pipes) to the output buffer. cat文件(和管道)打印到输出缓冲区。 This can probably also be done inside PHP. 这也可以在PHP内部完成。 cat is sometimes used for some fairly complex command line stuff which you may want to keep in the shell. cat有时用于某些相当复杂的命令行内容,您可能希望将它们保留在shell中。 But for simple stuff (and even moderately complex), PHP is perfectly capable. 但是对于简单的东西(甚至适度复杂的东西),PHP完全有能力。

It would help to have known more about what you're trying to achieve, but I believe PHP is capable of what it sounds like you're doing without having to use the shell at all. 了解更多您要实现的目标将有所帮助,但是我相信PHP能够完全听起来像您正在做的事情,而无需使用Shell。

Hope that helps. 希望能有所帮助。

使用passthru()而不是exec()

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

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