php exec: does not return output

前端 未结 10 1012
有刺的猬
有刺的猬 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:42

    I know I have choosen an answer as I must do it but I still do not understand why it wont work on my macchine but I found a fallback (using proc_open) solution using another method:

    public static function exec_alt($cmd)
    {
        exec($cmd, $output);
        if (!$output) {
            /**
             * FIXME: for some reason exec() returns empty output array @mine,'s machine.
             *        Somehow proc_open() approach (below) works, but doesn't work at
             *        test machines - same empty output with both pipes and temporary
             *        files (not we bypass shell wrapper). So use it as a fallback.
             */
            $output = array();
            $handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
            if (is_resource($handle)) {
                $output = explode("\n", stream_get_contents($pipes[1]));
                fclose($pipes[1]);
                proc_close($handle);
            }
        }
        return $output;
    }
    

    Hope this helps someone.

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

    Take a look at Apache error_log, maybe you'll find the error message.

    Also, you can try using popen instead of exec. It gives more control because you can separate process start from output read:

    $p = popen("dir", "r");
    if (!$p)
        exit("Cannot run command.\n");
    while (!feof($p))
        echo fgets($p);
    pclose($p);
    
    0 讨论(0)
  • 2020-12-01 17:55

    Try this:

    shell_exec('dir 2>&1');
    

    It works fine on Windows 8.1 64bits with UAC enabled.

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

    You can check permisssions of IIS User to cmd.exe

    cacls C:\WINDOWS\system32\cmd.exe
    

    If in output is line like (COMPUTERNAME=your computer name):

    C:\WINDOWS\system32\cmd.exe COMPUTERNAME\IUSR_COMPUTERNAME:N
    

    It means that user has no rights to execute cmd.

    You can add execute permissions with command:

    cacls C:\WINDOWS\system32\cmd.exe /E /G COMPUTERNAME\IUSR_COMPUTERNAME:R
    
    0 讨论(0)
提交回复
热议问题