php file_put_contents 函数不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4577149/
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
php file_put_contents function not working
提问by steve
Why would file_put_contents refuse to work for the following code?
为什么 file_put_contents 拒绝为以下代码工作?
$f = file_put_contents("files/r.js", "Some text here");
if ($f) print 1;
else print 0;
回答by Famver Tags
It could be a permission problem
可能是权限问题
Is the directory /files
chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.
目录/files
chmoded 为 777 吗?有时,如果没有足够的权限,php 将不允许您访问目录。我不知道空白错误。
Try to see if it has enough permissions, if not, then set it to 777 and try it.
试试看是否有足够的权限,如果没有,那就设置成777试试。
回答by jmort253
Are you using the full path on the filesystem or are you trying to use the URI? I think this PHP function expects you to give the path as the file is found on the filesystem.
您是使用文件系统上的完整路径还是尝试使用 URI?我认为这个 PHP 函数希望您在文件系统上找到文件时提供路径。
Relative paths should be okay though.
不过相对路径应该没问题。
You may need to make sure the file exists and that it's permissions are set to 777. Sometimes I've found that it's not enough to just have the directory permissions set to 777 but the file must also already exist.
您可能需要确保该文件存在并且其权限设置为 777。有时我发现仅将目录权限设置为 777 是不够的,但该文件也必须已经存在。
回答by designgroop
We've experienced this, requiring a workaround (regardless of method, permissions and anything else mentioned here). When all other fixes failed, we have found it can be linked to restrictions created by SELinux.
我们遇到过这种情况,需要一种解决方法(无论方法、权限和此处提到的任何其他内容)。当所有其他修复都失败时,我们发现它可以与 SELinux 创建的限制相关联。
回答by user2288580
If you're using windows the following solution worked perfectly for me on Windows 10 running php 5.5.38
如果您使用的是 Windows,以下解决方案在运行 php 5.5.38 的 Windows 10 上非常适合我
If you're having this problem on Windows/IIS try the following:
如果您在 Windows/IIS 上遇到此问题,请尝试以下操作:
- Go to the Folder that you are trying to write to and right click it, then select properties.
- Select the Security Tab
- Click Edit
- Click Add
- Click Advanced
- Click Find Now
- From the list of User select IUSR and click OK
- Click OK again.
- The IUSR will be displayed in the top box labelled 'Group of User Names'
- Select IUSR and grant necessary permissions in the 'Permissions for BATCH' list view.
- Click Apply and then you are done.
- 转到您要写入的文件夹并右键单击它,然后选择属性。
- 选择安全选项卡
- 单击编辑
- 点击添加
- 单击高级
- 点击立即查找
- 从用户列表中选择 IUSR 并单击确定
- 再次单击确定。
- IUSR 将显示在标有“用户名组”的顶部框中
- 选择 IUSR 并在“批处理权限”列表视图中授予必要的权限。
- 单击应用,然后您就完成了。
The steps may be slightly different for different versions of windows. This also applies to ASP.NET though I think the users you add are the network users (NETWORK AND OR NETWORK SERVICE users) as well as the IUSR.
对于不同版本的 Windows,这些步骤可能略有不同。这也适用于 ASP.NET,尽管我认为您添加的用户是网络用户(NETWORK AND OR NETWORK SERVICE 用户)以及 IUSR。
回答by Tuyen Nguyen
It is because of SELINUX.
这是因为 SELINUX。
You can mark the folder as of type httpd_sys_rw_content_twith this command to enable writing to this folder.
您可以使用此命令将文件夹标记为httpd_sys_rw_content_t类型以启用写入此文件夹。
semanage fcontext -a -t httpd_sys_rw_content_t '/files(/.*)?'; restorecon -RF '/files/'