Home » Php » php – Why can't write info into /tmp directory instead of /var/www/html?

php – Why can't write info into /tmp directory instead of /var/www/html?

Posted by: admin July 12, 2020 Leave a comment


LAMP installed on my local pc, as I know the string xxxx can be written into /tmp/test with below PHP function.

file_put_contents("/tmp/test","test1 test2") 

cat ajax_get.php

    $str = "test1 test2";
    ini_set('display_errors', 1);
    $str = implode(" ",$_GET);

Why the command file_put_contents("/tmp/test",$str); in ajax_get.php can’t work?

It is no use to replace file_put_contents with


Maybe it is an issue on directory permission, if I change below statement in ajax_get.php




And run the previous process, ajax_get.php create a file in /var/www/html/test

cat /var/www/html/test
test1 test2

Show the permission for /tmp directory.

ls -al /tmp
total 76
drwxrwxrwt 16 root    root    12288 Dec 10 18:39 .
drwxr-xr-x 23 root    root     4096 Dec  1 08:03 ..

. is the current directory /tmp, its permission is 777 (rwxrwxrwx),
why can’t write file into /tmp directory by PHP?

How to&Answers:

You are not sharing with us the return of file_put_contents("/tmp/test",$str);, but I’m going to assume that you are actually writing the appropriate bytes.

Being that the case, and considering the permissions look OK and that you don’t say that are getting an error message, the most likely scenario is a problem with systemd configuration.

Your Apache/PHP process are writing to that location correctly, but systemd has a configuration setting that allows for each process to have its own /tmp directory.

    Takes a boolean argument. If true sets up a new file system
     namespace for the executed processes and mounts a 
     private /tmp directory inside it, that is not shared by     
     processes outside of the namespace. This is useful to secure 
     access to temporary files of the process, but makes sharing
     between processes via /tmp impossible. 

Defaults to false.

In this case, the tmp you see as a regular or root user is not the same tmp directory that apache/php sees. Read more about this here, for example.

You could disable the PrivateTmp setting for Apache, but I think it would be easier to choose a different path to store your files, and give the apache/PHP process permission to write there.

There could be other possible explanations for not being able to write to tmp despite the directory permissions: e.g. that that directory was not added to the open_basedir directive, or that somehow the directory immutable attribute got set (very unlikely for /tmp). But in any of these cases, you would be getting an error message.


The return values of the file_get_contents method explains on the php manuel page as This function returns the number of bytes that were written to the file, or FALSE on failure. So file_get_contentsit’s must be a return. First, you have to check this returns.

Second, you wants create a file to a folder that looks like has a permission only root user. Apache user has not root permission, so you can’t write. Please check this url for permissions and I suggest to you be careful when you changing permission of folders.