file_put_contents 未创建 txt 文件

2022-01-13 00:00:00 linux centos php apache selinux

我目前有一个 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 尝试创建文件时,您会在 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

相关文章