Home  >  Q&A  >  body text

php - file_put_contents在apache上失效

代码

<?php

echo file_put_contents("/tmp/log.txt", "Hello world everyone.");

代码很简单,在浏览器和命令行上运行都显示成功,为:21。但是在浏览器运行后,/tmp/log.txt并不存在。而在命令行下,/tmp/log.txt创建成功(root和apache用户测试都没问题)。非常纠结。。。。。。。

操作系统: centos7

uname -r: 3.10.0-514.2.2.el7.x86_64

环境

都是通过yum install安装的,yum install httpd php5

黄舟黄舟2713 days ago706

reply all(7)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:05:18

    The /tmp directory should have the maximum permissions. Is there no permission problem? Open the error message and see the error message

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:05:18

    First, confirm the folder permissions to allow writing and then

    <?php
    echo file_put_contents($_SERVER['DOCUMENT_ROOT']."/tmp/log.txt", "Hello world everyone.");
    文件路径 如果不加 $_SERVER['DOCUMENT_ROOT'] 的话 要写成 "./tmp/log.txt" 这样  你在试试吧

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:05:18

    User questions? It is estimated that you are the root user under the command line, so the execution can be successful. Check to see if your web directory belongs to another user group, and this user group does not have read and write permissions on the temp directory.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:05:18

    Do you have permission? ! !

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:05:18

    It should be a permissions issue. It is also unsafe for PHP to operate the tmp directory, so it is definitely not allowed by default.
    Why is it said to be unsafe? Because if you can write, you must be able to read (for PHP, it is basically impossible to only write but not read without setting permissions)... In this case, hackers can read a lot of information

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:05:18

    Simple and crude test: chmod -R 777 /tmp

    Then see if it can be written.

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:05:18

    Browser running / refers to the website root directory. Command line / refers to the server root directory. Please check whether there is a /tmp/log.txt file in the website root directory

    reply
    0
  • Cancelreply