在 Apache 中授予 PHP 写入权限

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

Giving PHP write permission in Apache

phpapache2httpd.confapache2.2

提问by McKayla

I'm relatively new to configuring Apache.

我对配置 Apache 比较陌生。

I have a PHP script that writes a JSON file based on values retrieved from $_GET.

我有一个 PHP 脚本,它根据从$_GET.

<?php

    file_put_contents('State.json', "{ do: '" . $_GET['do'] . "' }");

    echo "Success";

?>

I run that code by create an XHR request.

我通过创建 XHR 请求来运行该代码。

Ally.xhr('/Cream/Foam?do=someCommand');

The page it returns says failed to open stream: Permission denied on line 3.

它返回的页面说 failed to open stream: Permission denied on line 3.

<Directory "~/Dropbox/Web">
    Options Indexes FollowSymLinks MultiViews

    AllowOverride None

    Order allow,deny
    Allow from all
</Directory>

Those are the permissions given to the root server folder.

这些是授予根服务器文件夹的权限。

What do I need to change to allow PHP to write the file?

我需要更改什么才能允许 PHP 写入文件?

(I have pretty much no idea what the block above means.)

(我几乎不知道上面的块是什么意思。)

回答by Jan Zyka

You need to check if the user under which runs apache has permission to write into the directory.

您需要检查运行apache的用户是否具有写入目录的权限。

So it's like this:

所以它是这样的:

Your apache server is process. The process runs under some user (say www). The PHP runs under apache. So if you try to write into a directory in PHP it is the same as if the user www logs into the server and tries to create a file in the same directory. So check who is owner of that directory and which permission do it have. You can do it e.g. via ls -lacommand. If wwwwill be owner of that directory, you will be 100% safe ...

您的 apache 服务器正在处理中。该过程在某个用户(例如 www)下运行。PHP 在 apache 下运行。因此,如果您尝试在 PHP 中写入目录,则与用户 www 登录服务器并尝试在同一目录中创建文件是相同的。因此,请检查谁是该目录的所有者以及它具有哪些权限。你可以通过ls -la命令来做到这一点。如果www将是该目录的所有者,您将是 100% 安全的...

回答by René H?hle

You can try to set the permissions with

您可以尝试设置权限

chmodfunction for php and set your directory to /var/www there you have normally enough permissions.

chmod函数用于 php 并将您的目录设置为 /var/www 那里您通常有足够的权限。

回答by Kit

Check the file permission either in command line using:

使用以下命令在命令行中检查文件权限:

ls -l /path/filename

Or through your ftp client if you have ftp access to the file/dir. If not, you could change the location like Stony said above.

或者,如果您对文件/目录具有 ftp 访问权限,则通过您的 ftp 客户端。如果没有,您可以像上面 Stony 所说的那样更改位置。

回答by Scott Duckworth

Check the file/directory permissions that it is trying to write to. Make sure that it is writable by the user and/or group that the Apache process is running as.

检查它试图写入的文件/目录权限。确保它可由运行 Apache 进程的用户和/或组写入。

Also check to see if SELinux is enabled by checking the contents of /selinux/enforce. If it is, either disable it or make sure the proper labels are set on the path that you are writing to.

还可以通过检查 /selinux/enforce 的内容来检查是否启用了 SELinux。如果是,请禁用它或确保在您写入的路径上设置了正确的标签。

回答by Christian

Know that sometimes there is absolutely no way to get around this using PHP only.

要知道,有时仅使用 PHP 绝对无法解决此问题。

The two solutions to this are:

对此的两种解决方案是:

  1. Configure PHP and Apache permissions manually (warning: can get dirty very quickly).
  2. Use FTP to change ownership to 0777 (full access) and then revert after running changes.
  1. 手动配置 PHP 和 Apache 权限(警告:可能会很快变脏)。
  2. 使用 FTP 将所有权更改为 0777(完全访问权限),然后在运行更改后恢复。

I've often found the latter option to work best.

我经常发现后一种选择效果最好。