gpt4 book ai didi

php - 为什么我不能写到/dev/stdout,但是php ://stdout works?

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:35 28 4
gpt4 key购买 nike

我正在尝试使用 PHP 和 Apache(在 Docker 的前台运行)写入 stdout(或 stderr)。

这些作品:

file_put_contents( "php://stderr","works" );
file_put_contents( "php://stdout","works" );

$fh = fopen('php://stdout','w'); fwrite($fh, "works");

但这些不是:

$stdout = fopen ("/dev/stdout","w"); fwrite($stdout, "fails");
$stderr = fopen ("/dev/stderr","w"); fwrite($stderr, "fails");

echo "fd1 exists:" . (file_exists('/proc/self/fd/1') ? 'yes' : 'no');
echo "fd1 writable:" . (is_writable('/proc/self/fd/1') ? 'yes' : 'no');
echo "stdout exists:" . (file_exists('/dev/stdout') ? 'yes' : 'no');
echo "stdout writable:" . (is_writable('/dev/stdout') ? 'yes' : 'no');

file_put_contents( "/proc/self/fd/1", "fails" );
file_put_contents( "/proc/self/fd/2", "fails" );
file_put_contents( "/dev/stdout", "fails" );

fwrite(STDOUT, 'fails');
fwrite(STDERR, 'fails');

/dev/stdout 确实存在,但不可写。 fopen('/dev/stdout') 也返回 FALSE。

fd1 exists:yes
fd1 writable:no
stdout exists:yes
stdout writable:no

更多信息:

  • 我尝试在 php.ini 中设置 error_log =/dev/stdout,但它不起作用
  • 我尝试了 Docker 命令 RUN ln -sf/dev/stdout stdout.log,然后用 PHP 写入 stdout.log,但是不行。

再一次,php://stdout 做了什么我没有做的?

最佳答案

原因是 Apache 将用户 fork 并更改为没有权限写入 /dev/stdout 的权限较低的用户(“apache”用户)。没有简单的方法解决这个问题,所以我最终得到了一个脚本。

该脚本创建一个文件(管道),该文件通过“cat”进程连接,该进程写入标准错误(作为 root):

mkfifo -m 600 logpipe
chown apache:apache logpipe
cat <> logpipe 1>&2 &
CAT=$!

# Start apache
/usr/sbin/httpd -D FOREGROUND

# Kill the cat after apache dies
kill $CAT

现在您可以在 php.ini 中设置 error_log = logpipe,所有 php 错误都将发送到 stderr。

至于为什么 php://stdout 起作用,我的猜测是 PHP 使用了一个已经被 Apache 打开的文件句柄(指向 /dev/stdout)。

更多详情 here .

关于php - 为什么我不能写到/dev/stdout,但是php ://stdout works?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56583510/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com