简体   繁体   English

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

[英]Why can't write info into /tmp directory instead of /var/www/html?

LAMP installed on my local pc, as I know the string xxxx can be written into /tmp/test with below PHP function. LAMP 安装在我的本地电脑上,因为我知道可以使用以下 PHP 函数将字符串xxxx写入/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);
?>

Why the command file_put_contents("/tmp/test",$str);为什么命令file_put_contents("/tmp/test",$str); in ajax_get.php can't work?ajax_get.php不能工作?

It is no use to replace file_put_contents withfile_put_contents替换为

$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也许这是目录权限的问题,如果我在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并运行前面的过程, ajax_get.php/var/www/html/test创建一个文件

cat /var/www/html/test
test1 test2

Show the permission for /tmp directory.显示/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 ..

. is the current directory /tmp , its permission is 777 (rwxrwxrwx), why can't write file into /tmp directory by PHP?是当前目录/tmp ,它的权限是 777 (rwxrwxrwx),为什么 PHP 不能将文件写入/tmp目录?

You are not sharing with us the return of file_put_contents("/tmp/test",$str);您没有与我们分享file_put_contents("/tmp/test",$str);的返回值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.在这种情况下,考虑到权限看起来不错,并且您没有说收到错误消息,最有可能的情况是systemd配置有问题。

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 .您的 Apache/PHP 进程正在正确写入该位置,但systemd 有一个配置设置,允许每个进程拥有自己的/tmp目录

 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.在这种情况下, tmp你看作为一个经常性或root用户是不一样的tmp目录的Apache / PHP的情思。 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.您可以禁用 Apache 的PrivateTmp设置,但我认为选择不同的路径来存储文件并授予 apache/PHP 进程在那里写入的权限会更容易。

There could be other possible explanations for not being able to write to tmp despite the directory permissions: eg that that directory was not added to the open_basedir directive, or that somehow the directory immutable attribute got set (very unlikely for /tmp ).尽管具有目录权限,但无法写入tmp可能还有其他可能的解释:例如,该目录未添加到open_basedir指令,或者以某种方式设置了目录不可变属性(对于/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. file_get_contents方法的返回值在php 手册页上解释为该函数返回写入文件的字节数,失败时返回 FALSE。 So file_get_contents it's must be a return.所以file_get_contents它必须是一个回报。 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.其次,您希望在一个看起来只有 root 用户权限的文件夹中创建一个文件。 Apache user has not root permission, so you can't write. Apache用户没有root权限,所以不能写。 Please check this url for permissions and I suggest to you be careful when you changing permission of folders.请检查此url的权限,我建议您在更改文件夹权限时要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM