简体   繁体   中英

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. 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,

ls, cat, 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.

ls is a directory listing. PHP provides a class called DirectoryIterator that can do exactly this in just a few lines of code:

An example:

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

pwd gets the current directory. PHP has a built-in function getcwd() that does this in a single line of code.

cat prints files (and pipes) to the output buffer. This can probably also be done inside PHP. cat is sometimes used for some fairly complex command line stuff which you may want to keep in the shell. But for simple stuff (and even moderately complex), PHP is perfectly capable.

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.

Hope that helps.

使用passthru()而不是exec()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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