Linux file_put_contents 不创建 txt 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14900719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_put_contents not creating txt file
提问by choppyfireballs
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?
我正在使用这一行尝试编写和创建文本文件,我知道我的文件位置有效,因为我可以运行它并在离线模式下创建它,IE 通过控制台运行它。问题是我尝试写入的变量是通过 HTTP Post 填充的,当我通过浏览器运行脚本时,或者当 apache 运行脚本时,它不会写入或创建文件。我需要做什么才能允许明智地访问写入/更改语法以使此脚本编写此文本文件?
采纳答案by Cory Klein
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
回答by Vlad
Have you tried chmodding the directory to 777
?
您是否尝试将目录修改为777
?
Try this:
尝试这个:
if(file_put_contents('file.txt', 'text')){
die('yes');
} else {
die('no');
}
Might of misspelled something. ^
可能拼错了一些东西。^
回答by vdegenne
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_contentswork 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 othersthe 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
这将授予 root 和其他人在该位置写入文件的权限)。如果它仍然像对我一样不起作用,那是因为您的系统可能是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
在这种情况下,请重新打开 selinuxsetenforce 1
并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.
或其他不同的东西,但httpd_sys_content_t
如果您曾经chcon
将上下文从一个目录转移到这个目录。但是如果你没有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) 永久 SELinux 布尔值设置httpd_anon_write
为 true,并且任何被称为 as 的上下文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 :
现在你必须说你的项目目录是 SELinuxpublic_content_rw_t
否则你仍然不能写文件。类型 :
semanage fcontext --add --type public_content_rw_t "/project(/.*)?"
and restorecon -RvF /project
to tell selinuxto apply the above specifications.
并restorecon -RvF /project
告诉selinux应用上述规范。
Now your directory is public_content_rw_tand you should be able to write files.
现在您的目录是public_content_rw_t并且您应该能够写入文件。
回答by Vender Aeloth
I'm note sure if my solution is really clear but it worked :
我很确定我的解决方案是否真的很清楚,但它有效:
cd /mydir/
setsebool -P allow_httpd_anon_write true
Best regards
此致
回答by Bob Reite
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 所有。
回答by Lorien Brune
Something else to try, for people with a similar question. You might just be making a simple mistake that doesn't require you to mess around with the file permissions—and if you're making this mistake, fixing the file permissions might not help.
对于有类似问题的人,可以尝试其他方法。你可能只是犯了一个简单的错误,不需要你搞乱文件权限——如果你犯了这个错误,修复文件权限可能无济于事。
Be sure you're using a local, relativefile path in file_put_contents().
确保您在 file_put_contents() 中使用本地的相对文件路径。
For example, use:
例如,使用:
file_put_contents('short_local_path/my_working_file.txt', $myString);
Not:
不是:
file_put_contents('http://example.com/remote_path/my_working_file.txt', $myString);
And not:
并不是:
file_put_contents('/whole/root/file/path/to/my_working_file.txt', $myString);