【问题标题】:Why can't write info into /tmp directory instead of /var/www/html?为什么不能将信息写入 /tmp 目录而不是 /var/www/html?
【发布时间】:2018-12-10 07:08:40
【问题描述】:

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

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);
?>

为什么ajax_get.php中的file_put_contents("/tmp/test",$str);命令不起作用?

file_put_contents替换成

是没有用的
$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);

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

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

进入

    file_put_contents("test",$str);

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

cat /var/www/html/test
test1 test2

显示/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 ..

.是当前目录/tmp,它的权限是777(rwxrwxrwx), 为什么PHP不能将文件写入/tmp目录?

【问题讨论】:

  • 检查在路径上创建新文件所需的权限,如果权限可用,则设置文件所需的权限以在其中写入内容。
  • 有错误信息吗?您尝试过什么调试?
  • 将文件写入代码放入TRY CATCH块中,检查是否发生异常。
  • @ManojSingh 没有一个使用过的函数一开始就抛出异常,所以在这一点上尝试捕获任何异常都没有什么意义。
  • @ManojSingh 这里使用的函数都可以抛出异常,所以一开始就使用 try/catch 毫无意义。

标签: php file-put-contents


【解决方案1】:

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

既然如此,考虑到权限看起来还不错,而且您没有说收到错误消息,最有可能的情况是systemd 配置有问题。

您的 Apache/PHP 进程正在正确写入该位置,但 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.

在这种情况下,您作为普通用户或 root 用户看到的 tmp 与 apache/php 看到的 tmp 目录不同。例如,阅读有关此here 的更多信息。

您可以禁用 Apache 的 PrivateTmp 设置,但我认为选择不同的路径来存储您的文件并授予 apache/PHP 进程写入权限会更容易。

尽管有目录权限,但无法写入 tmp 可能还有其他可能的解释:例如该目录未添加到open_basedir 指令中,或者以某种方式添加到the directory immutable attribute got set/tmp 不太可能)。但在任何这些情况下,您都会收到一条错误消息。

【讨论】:

  • 我不认为 OP 意味着文件没有出现在 /tmp 文件夹中,这是 PrivateTmp=true 会导致的。我认为他的意思是该文件根本没有写入,否则为什么要提到 /tmp 文件夹的权限是 drwxrwxrwt 并因此是可写的。但很难说出他的意思,因为我们不知道 file_put_contents() 的返回值。在我的情况下 PHP 无法写入 /tmp 并且 file_put_contents() 返回 false 即使权限设置为 drwxrwxrwt on /tmp 所以你的答案对我没有帮助。此行为在 Apache 自动更新到 Apache/2.4.29 后开始
  • @PHPGuru 考虑到 OP 接受了答案并授予了赏金,看来我得到了他们需要的答案。不知道为什么你认为你更了解“OP 的意思”。我很遗憾答案并没有解决您的问题。至少它可以通过消除另一个原因来帮助你。有了更多信息,其他人可能会帮助您。再见!
  • 我在这里问了我的问题:serverfault.com/questions/1069069/…
【解决方案2】:

file_get_contents 方法的返回值在 php manuel 页面上解释为 此函数返回写入文件的字节数,失败时返回 FALSE。 所以file_get_contents它一定是一个回报。首先,你必须检查这个回报。

其次,您想在一个看起来只有 root 用户权限的文件夹中创建一个文件。 Apache用户没有root权限,所以不能写。请检查此url 的权限,我建议您在更改文件夹权限时要小心。

【讨论】:

  • 如果你阅读了这个问题,使用的是file_PUT_contents,没有得到一个。并且文件夹 .. 是父级,而不是当前的。
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2014-01-10
  • 2013-04-18
相关资源
最近更新 更多