php exec: does not return output

前端 未结 10 1011
有刺的猬
有刺的猬 2020-12-01 17:20

I have this problem: On a ISS web server, windows 7 x64 professional, zend server installed. Running this command under php:

exec(\'dir\',$output, $err);


        
相关标签:
10条回答
  • 2020-12-01 17:34

    The main reason you get zero output from a command such as dir is because dir doesn't exist. It's part of the command prompt, and the solution to this particular problem is well, in this page somewhere.

    You can find this out by pressing WIN + R, and typing in dir or start for that matter - an error message appears!

    However perverse this may sound, I've found that the most reliable way to do anything process related in Windows is with using the Component Object Model. You said for us to share our experiences, right?

    $me hears people laughing

    Regained your composure yet?

    So, first we'll create the COM object:

    $pCOM = new COM("WScript.Shell");
    

    Then, we'll just run what ever needs to be run.

    $pShell = $pCom->Exec("C:\Random Folder\Whatever.exe");
    

    Cool! Now, that'll hang until everything's finished in that binary. So, what we need to do now is grab the output.

    $sStdOut = $pShell->StdOut->ReadAll;    # Standard output
    $sStdErr = $pShell->StdErr->ReadAll;    # Error
    

    There's some other things you can do - find out what was the process ID, error code, etc. Although this example is for Visual Basic, it's based on the same scheme.

    0 讨论(0)
  • 2020-12-01 17:34

    EDIT: After a few researchs, the only way i see to execute the DIR command is like that:

    cmd.exe /c dir c:\
    

    So you can try my solution using:

    $command = 'cmd.exe /c dir c:\\';
    

    You can use proc_open function too, which gives you access to stderr, return status code and stdout.

        $stdout = $stderr = $status = null;
        $descriptorspec = array(
           1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
           2 => array('pipe', 'w') // stderr is a pipe that the child will write to
        );
    
        $process = proc_open($command, $descriptorspec, $pipes);
    
        if (is_resource($process)) {
            // $pipes now looks like this:
            // 0 => writeable handle connected to child stdin
            // 1 => readable handle connected to child stdout
            // 2 => readable handle connected to child stderr
    
            $stdout = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
    
            $stderr = stream_get_contents($pipes[2]);
            fclose($pipes[2]);
    
            // It is important that you close any pipes before calling
            // proc_close in order to avoid a deadlock
            $status = proc_close($process);
        }
    
        return array($status, $stdout, $stderr);
    

    The interesting part with proc_open, compared to other functions like popen or exec, is that it provides options specific to windows platform like:

    suppress_errors (windows only): suppresses errors generated by this function when it's set to TRUE
    bypass_shell (windows only): bypass cmd.exe shell when set to TRUE
    
    0 讨论(0)
  • 2020-12-01 17:35

    There are a few posts to the relevant sections of the PHP Manual such as this one:

    I was having trouble using the PHP exec command to execute any batch file. Executing other commands (i.e., "dir") works fine). But if I executed a batch file, I receieved no output from the exec command.

    The server setup I have consists of Windows Server 2003 server running IIS6 and PHP 5.2.3. On this server, I have:

    1. Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
    2. Granted Everyone->Full Control to the directory in which the batch file is written.
    3. Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
    4. Granted the Internet User "log on as batch" permissions.
    5. Specified the full path to each file being executed.
    6. Tested these scripts running from the command line on the server and they work just fine.
    7. Ensured that %systemroot%\system32 is in the system path.

    It turns out that even with all of the above in place on the server, I had to specify the full path to cmd.exe in the exec call.

    When I used the call: $output = exec("c:\\windows\\system32\\cmd.exe /c $batchFileToRun");

    then everything worked fine. In my situation, $batchFileToRun was the actual system path to the batch file (i.e., the result of a call to realpath()).

    There are a few more on both the exec and shell_exec manual pages. Perhaps following through them will get it up and working for you.

    0 讨论(0)
  • 2020-12-01 17:38

    I was the same problem and after spend a lot of hours and cup of coffee

    Just Disabling User Account Control (UAC) in Windows 7 short path :P C:\Windows\System32\UserAccountControlSettings.exe and select "Never notify"

    and php exec() works fine!

    I use: Apache Version : 2.2.11
    PHP Version : 5.2.8
    Windows 7 SP1 32bit

    Best regards! :D

    0 讨论(0)
  • 2020-12-01 17:38

    For security purposes, your server may have restricted access to "exec" command. In this case, maybe you should contact the hosting company to remove this restriction (if there is one).

    0 讨论(0)
  • 2020-12-01 17:40

    If you are on Windows machine and trying to run an executable like this:

    shell_exec("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile\YourFile.exe");
    

    and there is no output or your file isn't started, then you should try this:

    chdir("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile");
    shell_exec("YourFile.exe");
    

    It should work.

    This is particularly handy if one is using relative path from folder where script currently is being located, for example:

    $dir = dirname(__FILE__).'\\..\\..\\..\\web\\';
    

    And don't forget to chdir() back to originating folder if you need to run other programs.

    0 讨论(0)
提交回复
热议问题