php exec: does not return output

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

    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
    

提交回复
热议问题