php file_put_contents 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4917811/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:16:45  来源:igfitidea点击:

file_put_contents permission denied

phpfile-iofile-permissions

提问by Hailwood

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.

我正在尝试将查询写入文件以进行调试。该文件位于database/execute.php. 我要写入的文件是database/queries.php.

I am trying to use file_put_contents('queries.txt', $query)

我正在尝试使用 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

file_put_contents(queries.txt) [function.file-put-contents]:无法打开流:权限被拒绝

I have the queries.txtfile chmod'd to 777, what could the issue be?

我将queries.txt文件 chmod 改为 777,可能是什么问题?

采纳答案by Jason

Try adjusting the directory permissions.

尝试调整目录权限。

from a terminal, run chmod 777 database(from the directory that contains the database folder)

从终端运行chmod 777 database(从包含数据库文件夹的目录)

apache and nobody will have access to this directory if it is chmodd'ed correctly.

如果 chmodd'ed 正确,则 apache 并且没有人可以访问此目录。

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.

另一件事是回显“getcwd()”。这将显示当前目录,如果这不是“/something.../database/”,那么您需要将“query.txt”更改为服务器的完整路径。

回答by azerafati

The other option

另一种选择

is that you can make Apache (www-data), the owner of the folder

是你可以制作Apache (www-data),文件夹的所有者

sudo chown -R www-data:www-data /var/www

that should make file_put_contentswork now. But for more security you better also set the permissions like below

现在应该可以file_put_contents工作了。但为了更安全,你最好也设置如下权限

find /var/www -type d -print0 | xargs -0 chmod 0755 # folder
find /var/www -type f -print0 | xargs -0 chmod 0644 # files
  • change /var/wwwto the root folder of your php files
  • 更改/var/www到您的 php 文件的根文件夹

回答by Adam

Realise this is pretty old now, but 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.

现在意识到这已经很老了,但是没有必要像这样手动将查询写入文件。MySQL 有内置的日志支持,你只需要在你的开发环境中启用它。

Take a look at the documentation for the 'general query log':

查看“一般查询日志”的文档:

http://dev.mysql.com/doc/refman/5.1/en/query-log.html

http://dev.mysql.com/doc/refman/5.1/en/query-log.html

回答by Hesam

Guys I had this problem for 1 month and did everything but couldn't fix it, but now I know the solution.

伙计们,我遇到了这个问题 1 个月并做了一切但无法解决它,但现在我知道了解决方案。

I use a shared linux hosting, when my admin changed the php to 5.3 I got many error for the "file_put_contents" code. try to test my plan:

我使用共享的 linux 主机,当我的管理员将 php 更改为 5.3 时,“file_put_contents”代码出现很多错误。尝试测试我的计划:

In your host create a file like mytest.php, and put this code in and save:

在你的主机上创建一个像 mytest.php 这样的文件,然后把这段代码放进去并保存:

<?php        mail('Your-EMail','Email-Title','Email-Message');        ?>

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 its from Nobodyyou have problem about "Permission Denied" because something not defined and if the sender name is like my id: [email protected] you dont have prob.

打开 URL“www.your-domain.com/mytest.php”一次,然后检查您的电子邮件。您应该收到一封来自您的主机的电子邮件,其中包含您在 mytest.php 中输入的信息,请检查发件人姓名。如果它来自没有人你有关于“权限被拒绝”的问题,因为没有定义的东西,如果发件人的名字像我的 id: [email protected] 你没有问题。

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.

我的管理员更改了服务器并再次安装了主机,我认为问题得到了解决,请告诉您的主机管理员我告诉您的内容,也许他们会找到答案。

hope it helps you!

希望对你有帮助!

回答by Bruce Tong

Gathering info from this link stackoverflow-image save doesn't work with chmod 777and from user azerafati and Loek Bergman

从此链接stackoverflow-image save收集信息不适用于 chmod 777以及来自用户 azerafati 和 Loek Bergman

if you were to look under /etc/apache/envvars file you will see something like:

如果您查看 /etc/apache/envvars 文件,您将看到如下内容:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

Apache is run under the username 'www-data'

Apache 在用户名“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:

'0755' 表示文件所有者可以读/写/执行但组和其他用户不能写。所以在你的终端中,cd 到包含你的“图像”文件夹的文件夹。然后输入:

find images -type d -exec chmod 0755 {} \;
find images -type f -exec chmod 0755 {} \;
sudo chown -R www-data:www-data images

you must change persmissions first before changing owner. enter your password when prompted. this will make 'www-data' owner of the images folder.

在更改所有者之前,您必须先更改权限。出现提示时输入您的密码。这将使“www-data”成为图像文件夹的所有者。

your upload should now work.

您的上传现在应该可以工作了。

回答by Loek Bergman

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.

我知道这是一个非常古老的问题,但我想添加一些深入的解释的好解决方案。您将不得不在类似 Ubuntu 的系统上执行两个语句,然后它就像一个魅力。

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.

Linux 中的权限可以用三位数字表示。第一个数字定义文件所有者的权限。第二个数字是特定用户组的权限。第三位数字定义了所有不是组所有者或组成员的用户的权限。

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.

网络服务器应该使用作为组成员的 id 执行。网络服务器不应以与文件和目录的所有者相同的 ID 运行。在 Ubuntu 中,在 id www-data 下运行 apache。该 id 应该是为其指定权限的组的成员。

To give the directory in which you want to change the content of files the proper rights, execute the statement:

要为要更改文件内容的目录赋予适当的权限,请执行以下语句:

find %DIR% -type d -exec chmod 770 {} \;

.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.

.这意味着在 OP 的问题中,目录 %ROOT%/database 的权限应该相应地更改。因此,重要的是不要在该目录中放置永远不应更改或删除的文件。因此,最佳做法是为必须更改内容的文件创建一个单独的目录。

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.

目录的读取权限 (4) 意味着能够收集目录中的所有文件和目录及其元数据。写权限 (2) 授予更改目录内容的权限。暗示添加和删除文件,更改权限等。执行权限(1)表示您有权进入该目录。没有后者,就不可能深入到目录中。当文件的内容应该被改变时,网络服务器需要读、写和执行权限。因此需要组的数字7。

The second statement is in the question of the OP:

第二个陈述是关于OP的问题:

find %DOCUMENT_ROOT%/database -type f -exec chmod 760 {} \;

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.

需要能够读写文档,但不需要执行文件。7 给文件的所有者,6 给组。网络服务器不需要具有执行文件的权限来更改其内容。这些写权限应该只授予该目录中的文件。

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:

对于不需要更改其文件的目录,组权限为 5 就足够了。有关权限的文档和一些示例:

https://wiki.debian.org/Permissions

https://wiki.debian.org/Permissions

https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

http://www.linux.org/threads/file-permissions-chmod.4094/

http://www.linux.org/threads/file-permissions-chmod.4094/

回答by d-_-b

For anyone using Ubuntu and receiving this error when loading the page locally, but not on a web hosting service,

对于使用 Ubuntu 并在本地加载页面时收到此错误的任何人,但不是在网络托管服务上,

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'

我只是通过打开 nautilus ( sudo nautilus) 并右键单击您尝试打开的文件来解决此问题,单击属性 > 设置 > 并为“其他人”提供读写权限

回答by DaveS

had the same problem; my issue was selinux was set to enforcing.

有同样的问题;我的问题是 selinux 设置为强制执行。

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.

即使在 chmoding 到 777 并确保所有父文件夹都具有 apache 用户的执行权限后,我仍然不断收到“无法打开流:权限被拒绝”错误。原来我的问题是 selinux 设置为强制执行(我在 centos7 上),这是一个 devbox,所以我将其关闭。

回答by rust

This can be resolved in resolved with the following steps :

这可以通过以下步骤解决:

1. $ php artisan cache:clear

2. $ sudo chmod -R 777 storage

3. $ composer dump-autoload

Hope it helps

希望能帮助到你

回答by Deepesh Thapa

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 .

如果您从 git 从本地拉取到服务器,有时您将需要清除缓存,因为它随它/或其他缓存文件一起上传的视图文件。

php artisan cache:clear

Sometimes it might just to the trick if your application was working before the git pull

有时,如果您的应用程序在 git pull 之前就可以工作,那么它可能只是为了解决问题