php - php exec 中的重定向在 apache + windows 7 中被破坏,在 windows XP 上工作

标签 php windows apache io-redirection

我一直在使用 PHP 在 Apache 服务器中执行遗留脚本。遗留脚本将调试数据写入 STDERR,我一直在根据调试设置将其重定向到黑洞或 STDOUT。

PHP 看起来有点像这样:

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

这在 XP 中可靠地工作。我得到了现在运行 windows7 的新硬件,然后回到这个代码,它被破坏了。零输出。返回代码 255。不知道为什么。

我能够让它再次运行的唯一方法是删除重定向。哦,重定向在终端盒中仍然可以完美地工作。

现在我必须从 apache-error-log(默认情况下每个 STDERR 输出都在此处)检索我的调试数据,这很不方便,但不是问题。

我只是想了解为什么重定向突然停止工作(并可能帮助其他遇到同样问题的人)。 apache 是一样的,事实上我只是从旧盒子中复制了 XAMPP 目录。错误?系统限制?操作系统策略禁止?

最佳答案

不使用 exec 和文件句柄重定向,而是使用 proc_open并实际捕获 stdout 和 stderr 的输出。与一些与进程相关的函数不同,proc_ 系列内置于所有版本的 PHP 中,并且在 Windows 上运行良好。

他们示例的完整性的 c&p:

$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\n";
}

请务必浏览文档页面上的upvoted 用户贡献的注释以了解可能的注意事项。

关于php - php exec 中的重定向在 apache + windows 7 中被破坏,在 windows XP 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146650/

相关文章:

php - Wordpress 表在 AJAX/PHP 后不会更新(即使有 200 ok 响应)

php - jquery从其他页面获取数据并输入表单字段

php - 如何在 Symfony 中使用 session ?

windows - 等效于通过 Team Foundation Server 命令行导入 svn

apache - .htaccess - 帮助重写 ssl 规则

php - zsh : command not found laravel

.net - 如何赋予程序比启动它的用户更多的权限

c# - 在 C# 中访问 Process.MainModule.FileName 时如何避免 Win32 异常?

php - getenv(ENVIRONMENT VARIABLE) 什么也没得到

windows - 在 apache/Linux 服务器上自动验证 Windows 用户