如何授予 PHP 对目录的写权限?

发布于 2024-09-02 23:28:31 字数 680 浏览 6 评论 0原文

我正在尝试使用 PHP 创建文件,但它不起作用。我假设这是因为它没有写访问权限(以前一直是这个问题)。我尝试通过使文件夹 chmod 0777 来测试这是否是问题所在,但这最终导致该目录中的每个脚本都返回 500 错误消息,直到我将其更改回来。 如何授予 PHP 对我的文件系统的写访问权限,以便它可以创建文件?

编辑:它托管在使用 Apache 的 Hostgator 共享托管上。

编辑2:有人询问代码: 该代码是GD图像脚本。我知道它的其余部分的工作原理就像以前我在每次调用它时创建图像一样。现在我尝试在添加新文本时创建它们并将它们保存到文件夹中。我的写入行是: imagejpeg(null,$file,85);

我还创建了一个测试文件来检查它是否只是一个损坏的脚本(主要是从 tizag 复制的): http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (我不知道是否/如何正确地在此处发布代码。这是 PHP 脚本的内容,减去 PHP 标签。)

它返回 13,13,1 (单独的行),所以看起来好像它认为它写了一些东西,但 testfile.txt 是空白的(我上传了一个空白的),或者不存在(如果我删除它)。

编辑3:服务器运行CentOS。

I'm trying to use PHP to create a file, but it isn't working. I am assuming this is because it doesn't have write access (it's always been the problem before). I tried to test if this was the problem by making the folder chmod 0777, but that just ended up making every script in that directory return a 500 error message until I changed it back.
How do I give PHP write access to my file system so it can a create a file?

Edit: It is hosted on Hostgator shared hosting using Apache.

Edit 2: Someone asked for the code:
The code is a GD image script. I know the rest of it works as previously I was creating the image every ime it was called. Now I am trying to create them when new text is added and save them to a folder. The write line I have is:
imagejpeg(null,$file,85);

I also created a test file to check if it was just a broken script (mainly copied from tizag):
http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (I don't know if/how to post the code here properly. Here is the contents of the PHP script, minus PHP tags.)

It returns 13,13,1 (separate lines), so it looks as if it thinks it wrote something, but the testfile.txt is blank (I uploaded a blank one), or non-existent (if I delete it).

Edit 3: The server runs CentOS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

红焚 2024-09-09 23:28:31

一个简单的方法是让 PHP 首先创建目录本身。

<?php
 $dir = 'myDir';

 // create new directory with 744 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
     mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');

这可以为您省去权限方面的麻烦。

An easy way is to let PHP create the directory itself in the first place.

<?php
 $dir = 'myDir';

 // create new directory with 744 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
     mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');

This saves you the hassle with permissions.

撩人痒 2024-09-09 23:28:31

简单的三步解决方案

摘要:您需要将目录的所有者设置为 PHP 使用的用户(Web 服务器用户)


第 1 步:确定 PHP 用户

创建一个包含以下内容的 PHP 文件:

<?php echo `whoami`; ?>

将其上传到您的 Web 服务器。输出应类似于以下内容:

www-data

因此,PHP 用户是 www-data


第 2 步:确定目录所有者

接下来,通过命令行检查 Web 目录的详细信息:

ls -dl /var/www/example.com/public_html/example-folder

结果应类似于以下内容:

drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder

因此,目录的所有者为 exampleuser1< /代码>。


第 3 步:将目录所有者更改为 PHP 用户

然后,将 Web 目录的所有者更改为 PHP 用户:

sudo chown -R www-data /var/www/example.com/public_html/example-folder

验证 Web 目录的所有者是否已更改:

ls -dl /var/www/example.com/public_html/example-folder

结果应类似于以下内容:

drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder

因此,example-folder的所有者已成功更改为PHP用户:www-data


完成! PHP 现在应该能够写入该目录。

Simple 3-Step Solution

Abstract: You need to set the owner of the directory to the user that PHP uses (web server user).


Step 1: Determine PHP User

Create a PHP file containing the following:

<?php echo `whoami`; ?>

Upload it to your web server. The output should be similar to the following:

www-data

Therefore, the PHP user is www-data.


Step 2: Determine Owner of Directory

Next, check the details of the web directory via the command line:

ls -dl /var/www/example.com/public_html/example-folder

The result should be similar to the following:

drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder

Therefore, the owner of the directory is exampleuser1.


Step 3: Change Directory Owner to PHP User

Afterwards, change the owner of the web directory to the PHP user:

sudo chown -R www-data /var/www/example.com/public_html/example-folder

Verify that the owner of the web directory has been changed:

ls -dl /var/www/example.com/public_html/example-folder

The result should be similar to the following:

drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder

Therefore, the owner of example-folder has successfully been changed to the PHP user: www-data.


Done! PHP should now be able to write to the directory.

吹泡泡o 2024-09-09 23:28:31

将目录的所有者设置为运行 apache 的用户。通常 Linux 上没有人

chown nobody:nobody <dirname>

这样,你的文件夹将无法被所有人写入,但对于 apache 来说仍然可写:)

Set the owner of the directory to the user running apache. Often nobody on linux

chown nobody:nobody <dirname>

This way your folder will not be world writable, but still writable for apache :)

∞梦里开花 2024-09-09 23:28:31

首先使用以下命令找出哪个用户拥有 httpd 进程,

ps aux | grep httpd

您将得到如下几行响应:

phpuser   17121  0.0  0.2 414060  7928 ?        SN   03:49   0:00 /usr/sbin/httpd

这里第一列显示用户名。现在您知道了正在尝试写入文件的用户,在本例中就是 phpuser
现在,您可以继续设置 php 脚本尝试写入内容的目录的权限:

sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere

1st Figure out which user is owning httpd process using the following command

ps aux | grep httpd

you will get a several line response like this:

phpuser   17121  0.0  0.2 414060  7928 ?        SN   03:49   0:00 /usr/sbin/httpd

Here 1st column shows the user name. So now you know the user who is trying to write files, which is in this case phpuser
You can now go ahead and set the permission for directory where your php script is trying to write something:

sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere
洒一地阳光 2024-09-09 23:28:31

您可以使用 PHP 的 chmod() 更改文件夹的权限。有关如何使用该命令的更多信息,请参见:http://php.net/manual /en/function.chmod.php

如果您在将权限设置为 777(全局可写)时收到 500 错误,则表示您的服务器已设置为阻止执行此类文件。这样做是出于安全原因。在这种情况下,您将需要使用 755 作为文件的最高权限。

如果在执行 PHP 文档的文件夹中生成了 error_log 文件,您将需要查看最后几个条目。这将使您了解脚本失败的地方。

为了获得 PHP 文件操作的帮助,我使用 http://www.tizag.com/phpT/filewrite .php 作为资源。

You can change the permissions of a folder with PHP's chmod(). More information on how to use the command is here: http://php.net/manual/en/function.chmod.php

If you get a 500 Error when setting the permissions to 777 (world writable), then it means your server is setup to prevent executing such files. This is done for security reasons. In that case, you will want to use 755 as the highest permissions on a file.

If there is an error_log file that is generated in the folder where you are executing the PHP document, you will want to view the last few entries. This will give you an idea where the script is failing.

For help with PHP file manipulation, I use http://www.tizag.com/phpT/filewrite.php as a resource.

败给现实 2024-09-09 23:28:31

我发现使用 HostGator,您必须将文件设置为 CMOD 644,将文件夹设置为 755。因为我是根据他们的技术支持这样做的,所以它可以与 HostGator 一起使用

I found out that with HostGator you have to set files to CMOD 644 and Folders to 755. Since I did this based on their tech support it works with HostGator

走过海棠暮 2024-09-09 23:28:31

我遇到了同样的问题:

由于我不愿意将 0777 给我的 php 目录,我创建了一个具有权限 0777 的 tmp 目录,我在其中创建了我需要写入的文件。

我的 php 目录继续受到保护。如果有人破解 tmp 目录,该网站将继续照常工作。

I had the same problem:

As I was reluctant to give 0777 to my php directory, I create a tmp directory with rights 0777, where I create the files I need to write to.

My php directory continue to be protected. If somebody hackes the tmp directory, the site continue to work as usual.

泅渡 2024-09-09 23:28:31

您可以将selinux设置为permissive以便进行分析。

    # setenforce 0

Selinux 将记录但允许访问。因此,您可以检查 /var/log/audit/audit.log 了解详细信息。也许您需要更改 selinux 上下文。为此,您将使用 chcon 命令。如果您需要,请向我们展示您的 audit.log 以获取更详细的答案。

解决问题后不要忘记启用selinux。最好保持 selinux 的强制执行。

    # setenforce 1

You can set selinux to permissive in order to analyze.

    # setenforce 0

Selinux will log but permit acesses. So you can check the /var/log/audit/audit.log for details. Maybe you will need change selinux context. Fot this, you will use chcon command. If you need, show us your audit.log to more detailed answer.

Don't forget to enable selinux after you solved the problem. It better keep selinux enforced.

    # setenforce 1
七颜 2024-09-09 23:28:31

授予对目录的写访问权限的最佳方法..

$dst = "path/to/directory";
mkdir($dst); 
chown($dst, "ownername");
chgrp($dst, "groupname");
exec ("find ".$dst." -type d -exec chmod 0777 {} +");

Best way in giving write access to a directory..

$dst = "path/to/directory";
mkdir($dst); 
chown($dst, "ownername");
chgrp($dst, "groupname");
exec ("find ".$dst." -type d -exec chmod 0777 {} +");
≈。彩虹 2024-09-09 23:28:31

我正在运行 Ubuntu,正如上面所说,没有人:没有人不能在 Ubuntu 上工作。您收到错误:

chown: invalid group: 'nobody:nobody'

相反,您应该使用“nogroup”,例如:

chown nobody:nogroup <dirname>

I'm running Ubuntu, and as said above nobody:nobody does not work on Ubuntu. You get the error:

chown: invalid group: 'nobody:nobody'

Instead you should use the 'nogroup', like:

chown nobody:nogroup <dirname>
总以为 2024-09-09 23:28:31

chmod 不允许您设置文件的所有权。要设置文件的所有权,必须使用 chown 命令。

chmod does not allow you to set ownership of a file. To set the ownership of the file you must use the chown command.

定格我的天空 2024-09-09 23:28:31

小小的提示!

回声whoami

确保使用反引号
不是
单引号 ''

(当然现在后面的引号不会出现在这个编辑器中!哦,好吧,我试过了!)

Tiny little hint!

echo whoami;

make sure you use BACK QUOTES
NOT
single quotes ' '
!

(of course now the back quotes don't show up in this editor! oh well, I tried!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文