Why can't write info into /tmp directory instead of /var/www/html?
Asked Answered
S

2

6

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

<?php
    $str = "test1 test2";
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    $str = implode(" ",$_GET);
    file_put_contents("/tmp/test",$str);
    print($str);
?>

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

$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);

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

    file_put_contents("/tmp/test",$str);

Into

    file_put_contents("test",$str);

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?

Shifflett answered 10/12, 2018 at 7:8 Comment(12)
check required permissions to create new file on path, if permissions available then set required permissions of file to write contents in it.Annul
In linux,any user can write file into the directory /tmp.Shifflett
Are there any error messages? Whave have you tried to debug this?Reconstructionism
No any other error messages.Shifflett
put file writting code in TRY CATCH block to check if any exception occures.Annul
@ManojSingh none of the used functions throw exceptions in the first place, so trying to catch any does make little sense at this point.Juanitajuanne
@Juanitajuanne file handling code should be in a try catch blockAnnul
@ManojSingh none of the functions used here can throw an exception, so using try/catch makes no sense to begin with.Juanitajuanne
@Juanitajuanne it was just a suggestion to find out any possible exception... as i am in habbit to use try catch for code used for file handling or db operations.Annul
Show more info in my revised post,it is a issue on directory permission.Shifflett
Have you verified the return of file_put_contents("/tmp/test",$str);? E.g. $ret = file_put_contents("/tmp/test",$str);. This would tell you if you are actually writing any bytes or not.Astronomical
You really need to define what “can’t” means here.Limy
A
18

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.

PrivateTmp=
    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.

Astronomical answered 14/12, 2018 at 16:30 Comment(3)
I don't think the OP means that the file isn't showing up in the /tmp folder which is what PrivateTmp=true would cause. I think he means that the file is not writing at all or else why mention the permissions on the /tmp folder being drwxrwxrwt and thus writable. But it's hard to tell what he means because we do not know the return value of file_put_contents(). In my case PHP can't write to /tmp and file_put_contents() returns false even though the permissions are set to drwxrwxrwt on /tmp so your answer doesn't help me. This behavior began after Apache automatically updated to Apache/2.4.29Bauske
@PHPGuru Considering the OP accepted the answer and awarded the bounty, it seems I got what they needed with this answer. Not sure why you think you know better "what the OP meant". I'm sad the answer does not solve your issue. At least it helps you by eliminating another cause. With more information someone else might be able to help you. Bye!Astronomical
I asked my question here: serverfault.com/questions/1069069/…Bauske
P
-3

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.

Parma answered 14/12, 2018 at 14:45 Comment(1)
If you read the question, are using file_PUT_contents, not get one. And folder .. are the parent, not the current one.Irrawaddy

© 2022 - 2024 — McMap. All rights reserved.