file_put_contents php权限 [英] file_put_contents php permissions

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

问题描述

这是关于我的系统配置以及apache/php如何受到影响的问题.当我进入生产环境时,在我的开发服务器上运行的代码无法正常工作,这就是我将错误归结为问题的原因.我想在php中使用file_put_contents来在我的系统上简单地创建一个文件.这是我正在使用的非常简单的代码:

This is a question about my system configuration and how apache/php can be affected by that. Code that was working on my dev server failed to work when I moved to production and here is the problem I boiled the error down to. I want to use file_put_contents in php to simply make a file on my system. Here is the very simple code I am using:

print file_put_contents("testfile.txt","this is my test")?"made file":"failed";

不会引发任何错误,但始终会打印失败".当我在普通目录中运行该文件时,不会生成任何文件,但是当我将其移至具有777权限的目录中时,它将生成文件,但仍显示失败",并且不保存任何内容.如果我将php文件本身更改为具有777权限,则不会进行任何更改.如果我将目标文件(testtext.txt)更改为具有777权限,则没有任何变化.

No errors are thrown but it always prints "failed". When I run it in the normal directory no file is made but when I move it to a directory with 777 permission, it will make the file but still print "failed" and save nothing into it. If I change the php file itself to have 777 permission nothing changes. If I change the target file (testtext.txt) to have 777 permission nothing changes.

当我将php文件的所有者更改为与创建的testfile.txt相同时,没有任何变化.当我尝试相对路径与绝对路径时,没有任何变化.出于某种原因,我可以用php制作文件,但它始终为空,并且使用file_put_contents时无法写入任何内容.

The when I change the owner of the php file to be the same as the created testfile.txt nothing changes. When I try relative vs. absolute paths nothing changes. For some reason I can have php make the file but it is always empty and using file_put_contents I can't write anything in.

创建的文件位于"www-data"的所有者和组中,如果有帮助,我将充当root用户.我该怎么做才能使它正常工作?我认为我的php版本正在使用suhosin扩展名(如果与此有任何关系)(或者,如果您认为有办法在php或suhosin ini文件中对此进行修复,我也可以这样做).

The created file is in the owner and group of 'www-data' while I am acting as root if that helps. What can I do to get this working? I think that my version of php is using the suhosin extension if this could have anything to do with it (or if you think there is a way to fix this in php or suhosin ini files I can do that too).

最近几天我一直在搜寻这个游戏.

I've been hunting this for hours the last few days.

根据以下评论进行更新.当我在CLI模式下运行它时,它确实可以工作,但是由于我需要通过apache运行它,我该如何利用这一事实?这是否告诉我们有关停止文件写入的位置或原因?同样,file_put_contents调用实际上返回的是"false"而不是零.

updates based on the comments below. When I run it in CLI mode it does work but since I need to run it through apache how can I use this fact? Does this tell us something about where or what is stopping the file writing? Also the file_put_contents call is actually returning "false" and not zero.

推荐答案

您必须将apache用户的权限设置为777. Apache还需要能够创建"新文件.我敢肯定,如果您尝试写入具有777权限的文件,那么它将起作用.

You have to set permissions as 777 for the apache user. Apache also needs to be able to 'create' new files. I'm sure that if you tried writing to a file with 777 permissions that it would work.

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

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