file_put_contents - 无法打开流:权限被拒绝
我正在尝试将查询写入文件以进行调试。该文件位于database/execute.php
中。我要写入的文件是database/queries.php
。
我正在尝试使用 file_put_contents('queries.txt', $query)
但我得到
file_put_contents(queries.txt) [function.file-put-contents]: 无法打开流:权限 被拒绝
我将 queries.txt
文件 chmod 为 777,问题可能是什么?
I am trying to write a query to a file for debugging. The file is in database/execute.php
. The file I want to write to is database/queries.php
.
I am trying to use file_put_contents('queries.txt', $query)
But I am getting
file_put_contents(queries.txt) [function.file-put-contents]:
failed to open stream: Permission
denied
I have the queries.txt
file chmod'd to 777, what could the issue be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
尝试调整目录权限。
从终端运行 chmod 777 database(从包含数据库文件夹的目录)
apache,如果正确 chmodd,则没有人可以访问该目录。
另一件要做的事情是 echo“getcwd()”。这将显示当前目录,如果这不是“/something.../database/”,那么您需要将“query.txt”更改为服务器的完整路径。
Try adjusting the directory permissions.
from a terminal, run
chmod 777 database
(from the directory that contains the database folder)apache and nobody will have access to this directory if it is chmodd'ed correctly.
The other thing to do is echo "getcwd()". This will show you the current directory, and if this isn't '/something.../database/' then you'll need to change 'query.txt' to the full path for your server.
您可以让
Apache (www-data)
(该文件夹的所有者):这应该使
file_put_contents
现在可以工作。但为了更安全,您最好还设置如下权限:/var/www
更改为 php 文件的根文件夹You can make
Apache (www-data)
, the owner of the folder:that should make
file_put_contents
work now. But for more security you better also set the permissions like below:/var/www
to the root folder of your php files无需手动将查询写入这样的文件。 MySQL 内置了日志记录支持,您只需在开发环境中启用它即可。
查看“常规查询日志”的文档< /a>.
There's no need to manually write queries to a file like this. MySQL has logging support built in, you just need to enable it within your dev environment.
Take a look at the documentation for the 'general query log'.
从此链接收集信息 stackoverflow-image save 不适用于 chmod 777,来自用户 azerafati 和 Loek Bergman:
如果您要查看 /etc/apache/envvars 文件,您将看到类似以下内容:
Apache is run用户名“www-data”下的
“0755”表示文件所有者可以读/写/执行,但组和其他用户不能写。因此,在您的终端中,cd 到包含“images”文件夹的文件夹。然后键入:
在更改所有者之前,您必须先更改权限。
出现提示时输入您的密码。这将使“www-data”成为图像文件夹的所有者。
您的上传现在应该可以工作了。
Gathering info from this link stackoverflow-image save doesn't work with chmod 777 and from user azerafati and Loek Bergman:
If you were to look under the /etc/apache/envvars file, you will see something like:
Apache is run under the username 'www-data'
'0755' means the file owner can read/write/execute but group and other users cannot write. so in ur terminal, cd to the folder containing your 'images' folder. then type:
You must change permissions first before changing owner.
Enter your password when prompted. This will make 'www-data' owner of the images folder.
Your upload should now work.
我知道这是一个非常古老的问题,但我想添加一个好的解决方案和一些深入的解释。你必须在类似 Ubuntu 的系统上执行两条语句,然后它就可以正常工作了。
Linux中的权限可以用三位数字来表示。第一个数字定义文件所有者的权限。第二位数字表示特定用户组的权限。第三位数字定义非组所有者或成员的所有用户的权限。
网络服务器应该使用该组成员的 ID 来执行。网络服务器不应使用与文件和目录所有者相同的 ID 运行。在 Ubuntu 中,apache 在 id www-data 下运行。该 ID 应该是为其指定权限的组的成员。
要为要更改文件内容的目录授予适当的权限,请执行以下语句:
。这意味着在 OP 的问题中,应相应更改目录 %ROOT%/database 的权限。因此,重要的是不要在该目录中包含永远不应更改或删除的文件。因此,最佳实践是为必须更改内容的文件创建单独的目录。
目录的读取权限 (4) 意味着能够收集目录中的所有文件和目录及其元数据。写权限 (2) 授予更改目录内容的权限。意味着添加和删除文件、更改权限等。执行权限 (1) 意味着您有权进入该目录。如果没有后者,就不可能深入目录。当文件内容需要更改时,网络服务器需要读取、写入和执行权限。因此需要数字7的组。
第二个陈述是OP的问题:
需要能够读取和写入文档,但不需要执行该文件。 7 分配给文件的所有者,6 分配给组。网络服务器不需要拥有执行该文件的权限即可更改其内容。这些写权限只能授予该目录中的文件。
不应向所有其他用户授予任何权限。
对于不需要更改其文件的目录,组权限为 5 就足够了。
有关权限的文档和一些示例:
https://wiki.debian.org/Permissions
https://www.linux.com/learn/tutorials/309527-understanding-linux-文件权限
http://www.linux.org/threads /文件权限-chmod.4094/
I know that it is a very old question, but I wanted to add the good solution with some in depth explanation. You will have to execute two statements on Ubuntu like systems and then it works like a charm.
Permissions in Linux can be represented with three digits. The first digit defines the permission of the owner of the files. The second digit the permissions of a specific group of users. The third digit defines the permissions for all users who are not the owner nor member of the group.
The webserver is supposed to execute with an id that is a member of the group. The webserver should never run with the same id as the owner of the files and directories. In Ubuntu runs apache under the id www-data. That id should be a member of the group for whom the permissions are specified.
To give the directory in which you want to change the content of files the proper rights, execute the statement:
.That would imply in the question of the OP that the permissions for the directory %ROOT%/database should be changed accordingly. It is therefor important not to have files within that directory that should never get changed, or removed. It is therefor best practice to create a separate directory for files whose content must be changed.
Reading permissions (4) for a directory means being able to collect all files and directories with their metadata within a directory. Write permissions (2) gives the permission to change the content of the directory. Implying adding and removing files, changing permissions etc.. Execution permission (1) means that you have the right to go into that directory. Without the latter is it impossible to go deeper into the directory. The webserver needs read, write and execute permissions when the content of a file should be changed. Therefor needs the group the digit 7.
The second statement is in the question of the OP:
Being able to read and write a document is required, but it is not required to execute the file. The 7 is given to the owner of the files, the 6 to the group. The webserver does not need to have the permission to execute the file in order to change its content. Those write permissions should only be given to files in that directory.
All other users should not be given any permission.
For directories that do not require to change its files are group permissions of 5 sufficient.
Documentation about permissions and some examples:
https://wiki.debian.org/Permissions
https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions
http://www.linux.org/threads/file-permissions-chmod.4094/
这可以通过以下步骤解决:
希望有帮助
This can be resolved in resolved with the following steps :
Hope it helps
如果您从本地 git 拉取到服务器,有时您需要清除缓存,因为它会上传视图文件/或其他缓存文件。
有时候,如果你的应用程序在 git pull 之前就可以工作,这可能只是一个窍门
If you are pulling from git from local to server, you will need to clear cache sometimes because of the view files it gets uploaded with it / or other cached files .
Sometimes it might just to the trick if your application was working before the git pull
这可能会有所帮助。这对我有用。在终端中尝试
setenforce 0
this might help. It worked for me. try it in the terminal
setenforce 0
我使用共享 Linux 主机,当我的管理员将 php 更改为 5.3 时,我遇到了许多“file_put_contents”代码错误。尝试测试我的计划:
在您的主机中创建一个类似 mytest.php 的文件,然后放入此代码并保存:
打开 URL“www.your-domain.com/mytest.php”一次,然后检查您的电子邮件。您应该会收到一封来自主机的电子邮件,其中包含您在 mytest.php 中输入的信息,请检查发件人姓名。如果它来自Nobody,则您会遇到“权限被拒绝”的问题,因为未定义某些内容,并且如果发件人姓名类似于我的 ID:[电子邮件受保护] 您没有问题。
我的管理员更改了服务器并再次安装了主机,我想问题已经解决了,请告诉您的主机管理人员我告诉您的内容,也许他们会找到答案。
I use a shared Linux hosting, when my admin changed the php to 5.3 I got many errors for the "file_put_contents" code. try to test my plan:
In your host create a file like mytest.php, and put this code in and save:
Open the URL "www.your-domain.com/mytest.php" one time and then check your email. You should have an email from your host with the information you entered in mytest.php, check the sender name. If it's from Nobody you have problem about "Permission Denied" because something not defined and if the sender name is like my id: [email protected] you don't have a problem.
My admin changed the server and installed the host again I think and the problem got solved, tell your host administration what I told you and maybe they find the answer.
对于任何使用 Ubuntu 并在本地加载页面时收到此错误的人,但不是在网络托管服务上,
我只是通过打开 nautilus (
sudo nautilus
) 并右键单击您要加载的文件来解决此问题尝试打开,单击属性>设置>并给“其他人”读写权限For anyone using Ubuntu and receiving this error when loading the page locally, but not on a web hosting service,
I just fixed this by opening up nautilus (
sudo nautilus
) and right click on the file you're trying to open, click properties > Settings > and give read write to 'everyone else'请使用此命令授予存储/框架和日志的权限
如果您仍然遇到权限错误,
尝试这个让组写入日志
use this cammand to give permission for storage/framework and logs
if you still have a permission error
try this to give group to write in log
我停止了病毒扫描程序 (Avast)。这解决了问题!最终,Avast 似乎有一个勒索软件防护罩,阻止了对文档根文件夹的写入操作。为单个程序(PHP、Tesseract)添加屏蔽异常解决了这个问题!
I stopped the virus scanner (Avast). That solved the problem! It eventually appeared that Avast had a ransomware shield blocking the write actions to the documentroot folder(s). Adding the shield exceptions for the individual programs (PHP, Tesseract) solved the issue!
有同样的问题;我的问题是 selinux 被设置为强制执行。
即使在 chmoding 到 777 并确保所有父文件夹都具有 apache 用户的执行权限后,我仍然收到“无法打开流:权限被拒绝”错误。结果我的问题是selinux被设置为强制(我在centos7上),这是一个devbox,所以我把它关掉了。
had the same problem; my issue was selinux was set to enforcing.
I kept getting the "failed to open stream: Permission denied" error even after chmoding to 777 and making sure all parent folders had execute permissions for the apache user. Turns out my issue was that selinux was set to enforcing (I'm on centos7), this is a devbox so I turned it off.
我遇到了同样的问题,我正在使用 Laravel,所以我刚刚所做的是:
并修复了!
I ran into the same issue, I'm using Laravel, so what I just did was:
And fixed!
如果您正在处理的文件夹是隐藏的,函数 file_put_contents 将抛出权限被拒绝的错误。
确保未设置此选项。
if the folder you are working on is hidden the function file_put_contents will throw permission denied error.
make sure this option is not set.
这是解决方案。
从 URL 复制图像。
此 URL:
http://url/img.jpg
创建所需的路径,用
.jpg
完成名称Here the solution.
To copy an img from an URL.
this URL:
http://url/img.jpg
create the desired path finish the name with
.jpg
有 2 种方法可以解决此问题
1.使用
chmod 777 目录路径
。如果不起作用的话
2。只需提供文件
query.txt
的完整路径即可。There 2 way to resolve this issues
1. use
chmod 777 path-to-your-directory
.if it does not work then
2. simply provide the complete path of your file
query.txt
.此外,正如
php.net
中的file_put_contents 手册页
中所述,请注意命名问题。可能不起作用(即使它在语法上是正确的),但
可以。我在不同的 php 安装服务器上经历过这个。
Furthermore, as said in
file_put_contents man page
inphp.net
, beware of naming issues.may not work (even though it is correct on syntax), but
works. I experienced this on different php installed servers.