Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Exists in /tmp, but PHP returns "No such file or directory" on CentOS

Tags:

php

centos

In PHP, I'm receiving this: Warning: fopen(/tmp/mydir/file.txt): failed to open stream: No such file or directory when calling fopen ('/tmp/mydir/file.txt', "r");

However:

cd /tmp
ls -l .
drwxrwxr-x 2 user  4.0K Aug 19 14:09 mydir
ls -l mydir
-rw-rw-r-- 1 user 41K Aug 19 14:09 file.txt

If I try to do print_r(scandir('/tmp/')); I get an empty array. Doing print_r(scandir('/tmp/mydir/')); I get failed to open dir: No such file or directory. However, doing print_r(scandir('/home/user/')); returns failed to open dir: Permission denied. For some reason, directories in /tmp show no such file or directory, even though they exist. It's also not a permission issue, and if it were, it would (should) produce the permission error.

What am I missing here? Or is this a known bug?

like image 774
Rainman Noodles Avatar asked Aug 19 '15 19:08

Rainman Noodles


1 Answers

It could be an issue with PrivateTmp feature: it mounts /tmp and /var/tmp as visible only to that process so every process sees a different virtual /tmp folder.

If you want to change it, you can create /etc/systemd/system/php-fpm.service.d/private-tmp.conf with contents:

[Service] PrivateTmp=false

Source for the fix: Content of /tmp is not visible from PHP-FPM

like image 138
aghidini Avatar answered Oct 13 '22 21:10

aghidini