dqu3974 2013-08-09 12:26
浏览 105
已采纳

php exec中的重定向在apache + windows 7中被破坏,正在使用Windows XP

i have been using PHP to execute a legacy script in an Apache server. the legacy script writes debug data to STDERR and i have been redirecting that to a black-hole or STDOUT depending on the debug-settings.

the PHP looks a bit like this:

exec('perl -e "print 10; print STDERR 20" 2>&1', $output);

that was reliably working in XP. i got new hardware which now runs windows7 and coming back to this code it is broken. zero output. return-code 255. no idea why.

the only way i was able to get it going again was to remove the redirection. oh, redirection still works perfectly in a terminal-box.

now i have to retrieve my debug-data from the apache-error-log (where every STDERR output goes by default) which is inconvenient but not a problem.

i just want to understand why the redirect stopped working all of a sudden (and maybe help others running into the same problem). the apache is the same, in fact i just copied the XAMPP dir over from the old box. a bug? system-limitation? forbidden by OS-policy?

  • 写回答

2条回答 默认 最新

  • dornc9470 2013-08-12 04:44
    关注

    Instead of using exec and using filehandle redirection, use proc_open and actually capture the output of stdout and stderr. Unlike some of the process-related functions, the proc_ family is built in to all versions of PHP and work fine on Windows.

    A c&p of their example for completeness:

    $descriptorspec = array(
       0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
       1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
       2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
    );
    
    $cwd = '/tmp';
    $env = array('some_option' => 'aeiou');
    
    $process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
    
    if (is_resource($process)) {
        // $pipes now looks like this:
        // 0 => writeable handle connected to child stdin
        // 1 => readable handle connected to child stdout
        // Any error output will be appended to /tmp/error-output.txt
    
        fwrite($pipes[0], '<?php print_r($_ENV); ?>');
        fclose($pipes[0]);
    
        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);
    
        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $return_value = proc_close($process);
    
        echo "command returned $return_value
    ";
    }
    

    Be sure to browse the upvoted user-contributed notes on the documentation page for possible caveats.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决