尽管有 777 权限且没有 open_basedir 值,但无法使用 php 写入 /tmp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10752396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't write to /tmp with php, despite 777 permissions and no open_basedir value
提问by Anders Sundnes L?vlie
I'm trying to write a file to my /tmp directory (on an apache server) with the php fopen function, but it fails:
我正在尝试使用 php fopen 函数将文件写入我的 /tmp 目录(在 apache 服务器上),但失败了:
<?php
$handle = fopen("/tmp/test.txt", "x");
if ($handle)
echo "Success!";
else
print_r(error_get_last());
This returns the error message:
这将返回错误消息:
failed to open stream: No such file or directory.
无法打开流:没有这样的文件或目录。
The /tmpdirectory has permissions set to drwxrwxrwtand I can see that the web user is writing other files to it. Mysteriously, if I point the script to another folder with permissions 777, it returns success. But my open_basedirhas no value. I have safe_mode=onand allow_url_fopen=off, but I don't think that should explain it?
该/tmp目录的权限设置为drwxrwxrwt,我可以看到网络用户正在向其中写入其他文件。神秘的是,如果我将脚本指向另一个具有权限的文件夹777,它会返回成功。但我的open_basedir没有任何价值。我有safe_mode=onand allow_url_fopen=off,但我认为这不应该解释它吗?
This is PHP 5.3.10 on Apache Httpd 2.0.
这是 Apache Httpd 2.0 上的 PHP 5.3.10。
回答by Andrey Kartashov
I had exactly the same problem. PHP reported no problem with opening file in /tmp/myoutputfile, but no file was in that path. Then I did
我遇到了完全相同的问题。PHP 报告在 中打开文件没有问题/tmp/myoutputfile,但该路径中没有文件。然后我做了
find / -name "myoutputfile"
and found it in /tmp/systemd-…/myoutputfile.
I've found this articleon Google.
So, in my case, it was a Systemd and Apache Httpd combination. I hope this will help to someone.
并在/tmp/systemd-…/myoutputfile. 我在谷歌上找到了这篇文章。
所以,就我而言,它是 Systemd 和 Apache Httpd 的组合。我希望这会对某人有所帮助。
回答by untill
Your problem is likely caused by the combination of systemd and apache. It's a security feature called PrivateTmp, and obviously it's an opt out.
您的问题很可能是由 systemd 和 apache 的组合引起的。这是一个称为 PrivateTmp 的安全功能,显然它是一个选择退出。
If you don't want it, you can disable it like this:
如果你不想要它,你可以像这样禁用它:
- Outcomment the respective switch in
/etc/systemd/system/multi-user.target.wants/apache2.service:#PrivateTmp=true - Restart systemd:
sudo systemctl daemon-reload - Restart apache:
sudo systemctl restart apache2
- 注释相应的开关
/etc/systemd/system/multi-user.target.wants/apache2.service:#PrivateTmp=true - 重启 systemd:
sudo systemctl daemon-reload - 重启阿帕奇:
sudo systemctl restart apache2
回答by Rene Pot
According to the error message displayed, there is no folder /tmp/. Perhaps the tmp folder is somewhere else than the root?
根据显示的错误消息,没有文件夹/tmp/。也许 tmp 文件夹不是根目录?
This error will not show if the file actually doesn't exist, as it will attempt to create it.
如果文件实际上不存在,则不会显示此错误,因为它会尝试创建它。
Method xalso returns a warning if the file already exists. (doc: http://www.php.net/manual/en/function.fopen.php)
x如果文件已经存在,方法还会返回警告。(文档:http: //www.php.net/manual/en/function.fopen.php)
I think this also goes for another reason this could go wrong, is the user which executes PHP doesn't have rights to write in the /tmp/folder.
我认为这也有另一个可能出错的原因,即执行 PHP 的用户没有在/tmp/文件夹中写入的权限。
回答by philippe_b
Try to add /tmpto open_basedir. For example:
尝试添加/tmp到open_basedir. 例如:
php_admin_value open_basedir /some/path:/another/path:/tmp
I'm not sure this is the problem you actually faced, but I found your question while looking for this solution so I guess that might help someone else.
我不确定这是您实际遇到的问题,但我在寻找此解决方案时发现了您的问题,所以我想这可能对其他人有所帮助。

