file_put_contents没有创建txt文件

时间:2021-01-02 16:46:52

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.

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

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

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?

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

5 个解决方案

#1


10  

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:

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

cd "My working file location"
ls -l .

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.

输出中有三列显示目录的权限,所有者和组。很可能它们归root所有,并且没有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:

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

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

#2


1  

I had the same trouble recently and stumbled upon this question. Unfortunately choppyfireballs the OP said in a comment he found his own solution and just accepted an answer that wasn't helping any of us... Then after a search and a success to make file_put_contents work again I decided to share my solution.

我最近遇到了同样的麻烦,偶然发现了这个问题。不幸的是,OP在评论中说他找到了自己的解决方案并且接受了一个没有帮助我们任何人的答案......然后在搜索并成功使file_put_contents再次工作后我决定分享我的解决方案。

The permissions of my files and directories were ok to accept any writing (make sure your directories are chmod 757 this will give the root and others the grant to write files in the location). If it still doesn't work like it didn't for me, that's because your system is probably SELinux (Security Enhanced Linux) system.

我的文件和目录的权限可以接受任何写入(确保您的目录是chmod 757,这将给根和其他人授予在该位置写入文件)。如果它仍然不像我没有那样工作,那是因为你的系统可能是SELinux(安全增强Linux)系统。

If you want to make sure write setenforce 0 this will turn selinux to permissive mode, run your script again, if it works then it means the problem is well described.

如果你想确保写setenforce 0这将把selinux变成许可模式,再次运行你的脚本,如果它工作,那么这意味着问题得到了很好的描述。

In that case turn selinux on back setenforce 1 and try ls -Zl in the directory where the directory of your project is. this will give you a line like

在这种情况下,在setenforce 1上转动selinux并在项目目录所在的目录中尝试ls -Zl。这会给你一个像

drwx---r-x.  9  root   root  system_u:object_r:httpd_sys_content_t:s0  4096  Dec  8  00:25  project

or something different but httpd_sys_content_t if you used chcon to transfer the context from one directory to this one. but if you don't have httpd_sys_content_t it's ok because we need to change the context of that directory anyways.

如果您使用chcon将上下文从一个目录传输到此目录,则可能与httpd_sys_content_t不同。但是如果你没有httpd_sys_content_t就可以了,因为我们无论如何都需要改变那个目录的上下文。

first you need to accept any public_content_rw_t contexts to write file. Type

首先,您需要接受任何public_content_rw_t上下文来写入文件。类型

setsebool -P httpd_anon_write on

This will set (P)ermanently SELinux boolean httpd_anon_write to true and any context dubbed as public_content_rw_t will have the rights to write any files in their own location.

这会将(P)ermanently SELinux boolean httpd_anon_write设置为true,任何被称为public_content_rw_t的上下文都有权在自己的位置写入任何文件。

Now you have to say SELinux that your project directory is public_content_rw_t or you'll still not be able to write files. Type :

现在你必须说SELinux你的项目目录是public_content_rw_t,否则你仍然无法写文件。类型:

semanage fcontext --add --type public_content_rw_t "/project(/.*)?"

and restorecon -RvF /project to tell selinux to apply the above specifications.

和restorecon -RvF /项目告诉selinux应用上述规范。

Now your directory is public_content_rw_t and you should be able to write files.

现在您的目录是public_content_rw_t,您应该能够写入文件。

#3


0  

I ran into this problem too. In my case, I found that the ownership of the directory was wrong. For a typical Apache installation the directory should be owned by www-data:www-data, not root:root.

我也遇到了这个问题。就我而言,我发现目录的所有权是错误的。对于典型的Apache安装,该目录应归www-data所有:www-data,而不是root:root。

#4


-1  

I'm note sure if my solution is really clear but it worked :

我注意到我的解决方案是否非常明确但是有效:

cd /mydir/
setsebool -P allow_httpd_anon_write true 

Best regards

最好的祝福

#5


-2  

Have you tried chmodding the directory to 777?

你试过将目录chmod到777吗?

Try this:

尝试这个:

if(file_put_contents('file.txt', 'text')){
    die('yes');
} else {
    die('no');
}

Might of misspelled something. ^

可能拼写错误的东西。 ^

#1


10  

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:

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

cd "My working file location"
ls -l .

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.

输出中有三列显示目录的权限,所有者和组。很可能它们归root所有,并且没有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:

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

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

#2


1  

I had the same trouble recently and stumbled upon this question. Unfortunately choppyfireballs the OP said in a comment he found his own solution and just accepted an answer that wasn't helping any of us... Then after a search and a success to make file_put_contents work again I decided to share my solution.

我最近遇到了同样的麻烦,偶然发现了这个问题。不幸的是,OP在评论中说他找到了自己的解决方案并且接受了一个没有帮助我们任何人的答案......然后在搜索并成功使file_put_contents再次工作后我决定分享我的解决方案。

The permissions of my files and directories were ok to accept any writing (make sure your directories are chmod 757 this will give the root and others the grant to write files in the location). If it still doesn't work like it didn't for me, that's because your system is probably SELinux (Security Enhanced Linux) system.

我的文件和目录的权限可以接受任何写入(确保您的目录是chmod 757,这将给根和其他人授予在该位置写入文件)。如果它仍然不像我没有那样工作,那是因为你的系统可能是SELinux(安全增强Linux)系统。

If you want to make sure write setenforce 0 this will turn selinux to permissive mode, run your script again, if it works then it means the problem is well described.

如果你想确保写setenforce 0这将把selinux变成许可模式,再次运行你的脚本,如果它工作,那么这意味着问题得到了很好的描述。

In that case turn selinux on back setenforce 1 and try ls -Zl in the directory where the directory of your project is. this will give you a line like

在这种情况下,在setenforce 1上转动selinux并在项目目录所在的目录中尝试ls -Zl。这会给你一个像

drwx---r-x.  9  root   root  system_u:object_r:httpd_sys_content_t:s0  4096  Dec  8  00:25  project

or something different but httpd_sys_content_t if you used chcon to transfer the context from one directory to this one. but if you don't have httpd_sys_content_t it's ok because we need to change the context of that directory anyways.

如果您使用chcon将上下文从一个目录传输到此目录,则可能与httpd_sys_content_t不同。但是如果你没有httpd_sys_content_t就可以了,因为我们无论如何都需要改变那个目录的上下文。

first you need to accept any public_content_rw_t contexts to write file. Type

首先,您需要接受任何public_content_rw_t上下文来写入文件。类型

setsebool -P httpd_anon_write on

This will set (P)ermanently SELinux boolean httpd_anon_write to true and any context dubbed as public_content_rw_t will have the rights to write any files in their own location.

这会将(P)ermanently SELinux boolean httpd_anon_write设置为true,任何被称为public_content_rw_t的上下文都有权在自己的位置写入任何文件。

Now you have to say SELinux that your project directory is public_content_rw_t or you'll still not be able to write files. Type :

现在你必须说SELinux你的项目目录是public_content_rw_t,否则你仍然无法写文件。类型:

semanage fcontext --add --type public_content_rw_t "/project(/.*)?"

and restorecon -RvF /project to tell selinux to apply the above specifications.

和restorecon -RvF /项目告诉selinux应用上述规范。

Now your directory is public_content_rw_t and you should be able to write files.

现在您的目录是public_content_rw_t,您应该能够写入文件。

#3


0  

I ran into this problem too. In my case, I found that the ownership of the directory was wrong. For a typical Apache installation the directory should be owned by www-data:www-data, not root:root.

我也遇到了这个问题。就我而言,我发现目录的所有权是错误的。对于典型的Apache安装,该目录应归www-data所有:www-data,而不是root:root。

#4


-1  

I'm note sure if my solution is really clear but it worked :

我注意到我的解决方案是否非常明确但是有效:

cd /mydir/
setsebool -P allow_httpd_anon_write true 

Best regards

最好的祝福

#5


-2  

Have you tried chmodding the directory to 777?

你试过将目录chmod到777吗?

Try this:

尝试这个:

if(file_put_contents('file.txt', 'text')){
    die('yes');
} else {
    die('no');
}

Might of misspelled something. ^

可能拼写错误的东西。 ^