php file_get_contents("/tmp/some_file") not working php7.1 php file_get_contents("/tmp/some_file") not working php7.1 apache apache

php file_get_contents("/tmp/some_file") not working php7.1


I found issue answer here: https://serverfault.com/questions/614781/php-script-cant-access-tmp-folder?newreg=e8651c2bcaea4a718a38f4e1dc94805b

It is related to ubuntu 16.04 and apache. For apache service PrivateTmp=true in this configuration file /etc/systemd/system/multi-user.target.wants/apache2.service.

That is why during executing php file via browser+apache result was false and via terminal it was true, as in case apache executing virtal /tmp directory been generated where no /tmp/some_file exists


Try error_get_last()

Get the last occurred error

For example:

$content = file_get_contents("/tmp/some_file");if($content === false){    print_r(error_get_last());}

Or

$fp = fopen("/tmp/some_file", "r");if($fp === false){    print_r(error_get_last());}echo fread($myfile,filesize("/tmp/some_file"));fclose($myfile);


I have try "multi-user.target.wants" solution, it have worked but after restart, but at some point, PrivateTmp go back to true.Like my principal use of Apache2 is PHP, I finally edited php.ini and I've uncomment line sys_temp_dir.

By default system use temp dir assigned by function sys_get_temp_dir. Function sys_get_temp_dir will return "/tmp" but the truth is that your tmp files are storing at some path like /tmp/systemd-private-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-apache2.service-YYYYYY//tmp/*. So, what work for me was:

Edit php.ini (path can change between PHP versions)

sudo nano /etc/php/7.2/cli/php.ini

Then uncomment sys_temp_dir line

; Directory where the temporary files should be placed.; Defaults to the system default (see sys_get_temp_dir)sys_temp_dir = "/tmp"