file_put_contents 不创建 txt 文件 [英] file_put_contents not creating txt file

查看:53
本文介绍了file_put_contents 不创建 txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 php 脚本,当浏览器浏览器浏览到网页时,该脚本正在运行.我想要做的是在脚本运行时编写一个文本文件来存储变量.该文件夹的所有者是apache,但每个人都有读写权限,严格用于测试目的.(我认为这可能是权限问题)在服务器上启用了 SELINUX,当我从控制台运行脚本时,它会在正确的目录中创建文本文件.

I currently have a php script that is running when a browser browser browses to the webpage. What I'm trying to do is write a text file when the script runs that stores a variable. The owner of the folder is apache, but everyone has read write, strictly for testing purposes. (I thought it might be a permissions issue) SELINUX is enabled on the server, and when I run the script from console it creates the text file just fine, and in the right directory.

file_put_contents("My working file location", $myString);

我正在使用这一行来尝试编写和创建文本文件,我知道我的文件位置有效,因为我可以运行它并在离线模式下创建它,即通过控制台运行它.问题是我尝试写入的变量是通过 HTTP Post 填充的,当我通过浏览器运行脚本时,或者 apache 运行脚本时,它不会写入或创建文件.我需要做什么才能允许明智地访问写入/更改语法以使此脚本写入此文本文件?

I'm using this line to try to write and create the text file, I know that my file location works becaus I can run it and create it in offline mode, I.E. running it through console. The problem is that the variable I'm trying to write is populated through HTTP Post, and when I run the script through the browser, or when apache runs the script, it does not write or create the file. What do I need to do to allow access to write/change syntax wise to get this script to write this text file?

推荐答案

您的问题很可能是由于 apache 没有写入您指定的文件位置的权限.转到该目录并使用 ls 命令检查权限和组所有权:

Your problem is likely due to apache not having permissions to write to the file location you specified. Go to that directory and check the permissions and group ownership with the ls command:

cd "My working file location"
ls -l .

输出中有三列显示目录的权限、所有者和组.它们很可能归 root 所有,并且没有 apache 写入目录的权限.

There are three columns in the output that show the permissions, owner, and group for the directory. Most likely they are owned by root and don't have permissions for apache to write to the directory.

如果是这种情况,那么当它尝试创建文件时,您将看到 apache 日志中出现错误.在浏览器中运行脚本时尝试拖尾日志:

If this is the case, then you will see an error appear in your apache log when it tries to create the file. Try tailing your logs while running the script in your browser:

tail -f /var/log/apache2/error.log

这篇关于file_put_contents 不创建 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆