为什么不能将信息写入/ tmp目录而不是/ var/www/html?

scr*_*apy 7 php file-put-contents

LAMP安装在我的本地电脑上,因为我知道该字符串xxxx可以/tmp/test用下面的PHP函数写入.

file_put_contents("/tmp/test","test1 test2") 
Run Code Online (Sandbox Code Playgroud)

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);
?>
Run Code Online (Sandbox Code Playgroud)

为什么命令file_put_contents("/tmp/test",$str);ajax_get.php不能工作?

这是没有用的,以取代file_put_contents

$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);
Run Code Online (Sandbox Code Playgroud)

如果我更改下面的语句,可能是目录权限的问题 ajax_get.php

    file_put_contents("/tmp/test",$str);
Run Code Online (Sandbox Code Playgroud)

    file_put_contents("test",$str);
Run Code Online (Sandbox Code Playgroud)

并运行上一个进程,在其中ajax_get.php创建一个文件/var/www/html/test

cat /var/www/html/test
test1 test2
Run Code Online (Sandbox Code Playgroud)

显示/tmp目录的权限.

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 ..
Run Code Online (Sandbox Code Playgroud)

.是当前目录/tmp,其权限是777(rwxrwxrwx),为什么不能通过/tmpPHP 将文件写入目录?

yiv*_*ivi 13

您没有与我们分享返回file_put_contents("/tmp/test",$str);,但我将假设您实际上正在编写适当的字节.

就是这种情况,并考虑到权限看起来没问题,并且您没有说收到错误消息,最可能的情况是systemd配置问题.

您的apache/PHP进程正在写入该位置,但systemd具有允许每个进程拥有自己的systemd目录的配置设置.

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.
Run Code Online (Sandbox Code Playgroud)

在这种情况下,/tmp您看到的普通用户或root用户与tmpapache/php看到的目录不同.例如,在这里阅读更多相关信息.

您可以禁用tmpapache 的设置,但我认为选择一个不同的路径来存储您的文件会更容易,并且允许apache/PHP进程写入那里.

PrivateTmp尽管存在目录权限,但可能还有其他可能的解释:无法写入:例如,该目录未添加到open_basedir指令中,或者某种程度上目录不可变属性已设置(非常不可能tmp).但在任何这些情况下,您都会收到错误消息.