为什么PHP 7.2 fopen(/tmp,a)不写入文件? [英] Why doesn't PHP 7.2 fopen(/tmp, a) write to the file?

查看:65
本文介绍了为什么PHP 7.2 fopen(/tmp,a)不写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的"PHPDBG"功能,该功能使我可以"printf"到文本文件.

I have an old "PHPDBG" function that lets me "printf" to a text file.

我有 PHPDBG.inc 自永远"(至少自PHP 4.x以来),但是它似乎不适用于我当前的配置(ubuntu18,Apache 2.4) .29和PHP 7.2).

I've had PHPDBG.inc "since forever" (at least since PHP 4.x days), but it doesn't seem to be working in my current configuration (ubuntu18, Apache 2.4.29 and PHP 7.2).

特别是:

  • 我无法打开文件($ fp为空)...
  • /tmp/PHPDBG.txt永远不会创建(由于fopen失败)
  • /tmp应该是可写的 ...和...
  • 我似乎无法在Apache error.log中获得PHP错误,也无法从error_get_last()$php_errormsg中获得任何有意义的信息.
  • I can't open the file ($fp is null) ...
  • /tmp/PHPDBG.txt never gets created (because of the fopen failure)
  • /tmp should be world-writable ... and ...
  • I don't seem to be able to get a PHP error in the Apache error.log, or get anything meaningful from either error_get_last() or $php_errormsg.

这是测试代码:

test.php:

<?php
  function PHPDBG ($s) {
    $fp = fopen ("/tmp/PHPDBG.txt", "a");
    if ($fp) {
      // Successful open ... but nothing written!
      fputs($fp, $s . "\n");
      fclose($fp);
    } else {
      echo "<h3>FILE OPEN ERROR</h3>\n";
      echo "<p>" . print_r(error_get_last()) . "</p>\n";
      echo "<p>" . $php_errormsg . "</p>\n";
    }
  }

  PHPDBG('>>Hello');
  phpinfo();
  PHPDBG('<<Goodbye');
 ?>

问题:

问题1:您知道$fp = fopen ("/tmp/PHPDBG.txt", "a");可能有什么问题吗?

Q1: Any idea what might be wrong with $fp = fopen ("/tmp/PHPDBG.txt", "a");?

Q2:如果"fopen()"失败,我该怎么办才能得到有意义的错误消息?

Q2: What can I do to get a meaningful error message if "fopen()" fails?

其他信息:假定error_get_last()返回"1:EPERM 1/*不允许操作*/",然后我手动创建了/tmp/PHPDBG.txt、chmod + rw,然后再次尝试了"test.php".否:我得到的结果完全相同:$ fp为空,没有有意义的错误消息,并且/tmp/PHPDBG.txt不变:

Additional info: assuming error_get_last() is returning "1: EPERM 1 /* Operation not permitted */", then I manually created /tmp/PHPDBG.txt, chmod +rw, and tried "test.php" again. No-go: I got exactly the same results: $fp was null, no meaningful error messages, and /tmp/PHPDBG.txt was unchanged:

root@ubuntu18:/tmp# umask 0
root@ubuntu18:/tmp# touch PHPDBG.txt
root@ubuntu18:/tmp# ls -l PHPDBG.txt
-rw-rw-rw- 1 root root 0 Mar  5 18:13 PHPDBG.txt
 <= Re-ran test here... failed exactly like before...
root@ubuntu18:/tmp# ls -l PHPDBG.txt
-rw-rw-rw- 1 root root 0 Mar  5 18:13 PHPDBG.txt


附加说明:

  1. Ibu在我发布的代码的原始版本中指出了一个愚蠢的错字.哎呀!它在最后一分钟出现,是打字错误 WAS N'T 的问题.我仍然无法在/tmp中打开()"文件并从PHP 7.2对其进行写入.我曾经能够在PHP的早期版本(更早!)中做到这一点.

  1. Ibu pointed out a stupid typo in the original version of the code I posted. Whoops! It crept in at the last minute, the typo WASN'T the problem. I'm still not able to "fopen()" a file in /tmp and write to it from PHP 7.2. I used to be able to do this in earlier (MUCH earlier!) versions of PHP.

我只是仔细检查了一下:如果文件 AM 恰好位于本地目录中,我可以写该文件:

I just double-checked: I AM able to write to the file if it happens to be in the local directory:

// $fp = fopen ("/tmp/PHPDBG.txt", "a");  // Opens, but fails to write anything
$fp = fopen ("PHPDBG.txt", "a");  // Works OK

问:为什么????

更新

它曾经起作用"的原因是systemd引入了Linux(较新的版本),并附带了"PrivateTmp".

The reason "it used to work" is that systemd was introduced to (newer versions of) Linux, bringing with it "PrivateTmp".

我的解决方法是为Apache/PHP 禁用此功能".我对/etc/systemd/system/multi-user.target.wants/apache2.service进行了如下

My workaround was to disable this "feature" for Apache/PHP. I edited /etc/systemd/system/multi-user.target.wants/apache2.service as follows:

[Service]
...
PrivateTmp=true  <-- Changed this to "false"

其他说明位于此处.

推荐答案

我发现似乎没有创建的文件:

I found the file that didn't seem to be created:

  1. PHP代码:$fp = fopen ("/tmp/PHPDBG.txt", "a");

预期位置:/tmp/PHPDBG.txt.

实际位置:/tmp/systemd-private-c6f7629309e647818680f8a6ee1105d6-apache2.service-lGKGc6/tmp/PHPDBG.txt

相关链接:

  • 为什么PHP无法看到/tmp

    所以听起来这是某种系统化的功能"(Grrr !!!!).这就解释了为什么它可用"(在Apache,PHP和Linux的早期版本中).

    So it sounds like this is some kind of systemd "feature" (Grrr!!!!). Which explains why it "used to work" (in previous versions of Apache, PHP and Linux).

    解决方法

    我编辑了/etc/systemd/system/multi-user.target.wants/apache2.service:

    [Service]
    ...
    PrivateTmp=true  <-- Changed this to "false"
    

    这篇关于为什么PHP 7.2 fopen(/tmp,a)不写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆