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 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch