Can't write to /tmp with php, despite 777 permissions and no open_basedir value
Asked Answered
A

4

45

I'm trying to write a file to my /tmp directory (on an apache server) with the php fopen function, but it fails:

<?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 /tmp directory has permissions set to drwxrwxrwt and 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_basedir has no value. I have safe_mode=on and allow_url_fopen=off, but I don't think that should explain it?

This is PHP 5.3.10 on Apache Httpd 2.0.

Aglitter answered 25/5, 2012 at 10:3 Comment(3)
No, the point of the above script is to test whether I can create files in my /tmp folder. I have tried the script with both the 'x' and the 'w' flags.Dugger
Thanks Robus - that sounds quite possible - the server is on a large, fairly complex university network. How could I verify whether that's the issue?Dugger
Try doing an "ls -l /tmp" or the equivalent. I'm running into this issue on Perl, and the /tmp directory the script is seeing is very different from what's in my actual /tmp directory.Whole
F
67

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

find / -name "myoutputfile"

and found it in /tmp/systemd-…/myoutputfile. I've found this article on Google.
So, in my case, it was a Systemd and Apache Httpd combination. I hope this will help to someone.

Fenton answered 17/4, 2014 at 19:37 Comment(4)
This is huge! Caused a hard to find bug in a Drupal RHEL 6 to RHEL 7 upgrade where the Drupal file_temp_directory was set to /var/tmp/drupal but images styles were failing to be generated because the drupal directory had not been created yet. Once going to an admin page (e.g. admin/config) where it made the directory then image styles were generated properly. Restarting the server caused the bug to appear again, but only realized that in hindsight, was incredibly hard to reproduce.Land
When using PHP-FPM, remeber checking the PrivateTmp flag in the php-fpm.service unit rather than the Httpd or NGINX ones.Nerves
that doesn't explain the "no such file or directory" error in the original question thoughTrefler
How is this problem even remotely the same. The OP says he can't write to /tmp and you are clearly writing to /tmp just fine. It's just mapped to a different folder because of a PrivateTmp setting. Sounds to me like an altogether different problem!Operable
G
24

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.

If you don't want it, you can disable it like this:

  1. Outcomment the respective switch in /etc/systemd/system/multi-user.target.wants/apache2.service: #PrivateTmp=true
  2. Restart systemd: sudo systemctl daemon-reload
  3. Restart apache: sudo systemctl restart apache2
Goulden answered 21/11, 2019 at 9:57 Comment(2)
/etc/systemd/system/multi-user.target.wants/httpd.service for me on Centos 7 and Apache 2.4.Ostrander
PrivateTmp isn't supposed to break your PHP scripts.Operable
L
1

Try to add /tmp to open_basedir. For example:

    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.

Lecroy answered 16/3, 2014 at 9:34 Comment(0)
N
0

According to the error message displayed, there is no folder /tmp/. Perhaps the tmp folder is somewhere else than the root?

This error will not show if the file actually doesn't exist, as it will attempt to create it.

Method x also returns a warning if the file already exists. (doc: 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.

Negligible answered 25/5, 2012 at 10:12 Comment(1)
As I wrote in the question, there IS a folder called /tmp on my server, yes it resides on the root, and from what I can tell the user which executes php does write files to the /tmp folder continuously.Dugger

© 2022 - 2024 — McMap. All rights reserved.