如何给PHP写访问目录?

时间:2022-04-05 06:48:15

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?

我试图使用PHP创建一个文件,但它不能工作。我假设这是因为它没有写访问(以前总是有问题)。我试着测试这是不是问题,我做了一个文件夹chmod 0777,但那只是让那个目录中的每个脚本返回一个500错误信息,直到我把它改回来。我如何给PHP写访问我的文件系统,让它可以创建一个文件?

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

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

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);

编辑2:有人要代码:代码是一个GD图像脚本。我知道它的其余部分就像我之前创建的每一个ime那样工作。现在我尝试在添加新文本并将它们保存到文件夹时创建它们。我的写行是: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.)

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

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

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

Edit 3: The server runs CentOS.

编辑3:服务器运行CentOS。

11 个解决方案

#1


45  

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

一种非常简单的方法是让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) ) {
     $oldmask = umask(0);  // helpful when used in linux server  
     mkdir ($dir, 0744);
 }

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

This saves you the hassle with permissions.

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

#2


31  

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

将目录的所有者设置为运行apache的用户。在linux上往往没有人

chown nobody:nobody <dirname>

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

这样,您的文件夹将不会是可写的,但仍然可以为apache编写:)

#3


6  

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

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

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:

这里的第一列显示用户名。现在你知道了要写文件的用户,也就是phpuser你现在可以继续设置你的php脚本要写东西的目录的权限:

sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere

#4


3  

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

可以使用PHP的chmod()更改文件夹的权限。关于如何使用该命令的更多信息如下: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.

如果您在将权限设置为777 (world writable)时出现500个错误,那么这意味着您的服务器将设置为防止执行此类文件。这是出于安全考虑。在这种情况下,您将希望使用755作为文件的最高权限。

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.

如果在执行PHP文档的文件夹中生成一个error_log文件,您将希望查看最后几个条目。这将使您了解脚本在哪里出错。

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

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

#5


2  

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

我发现,在HostGator中,您必须将文件设置为CMOD 644和文件夹到755。因为我是基于他们的技术支持做的,所以它可以和HostGator一起工作

#6


2  

Simple 3-Step Solution

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

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


Step 1: Determine PHP User

步骤1:确定PHP用户

Create a PHP file containing the following:

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

<?php echo `whoami`; ?>

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

将它上载到您的web服务器。输出应类似于以下内容:

www-data

Therefore, the PHP user is www-data.

因此,PHP用户是www-data。


Step 2: Determine Owner of Directory

步骤2:确定目录的所有者

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

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

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.

因此,目录的所有者是exampleuser1。


Step 3: Change Directory Owner to PHP User

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

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

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

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

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

验证web目录的所有者已被更改:

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.

因此,示例文件夹的所有者已经成功地更改为PHP用户:www-data。


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

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

#7


1  

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

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

#8


1  

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.

由于我不愿意将0777发送到我的php目录,所以我创建了一个tmp目录,其中包含了权利0777,在这里我创建了需要写入的文件。

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

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

#9


1  

You can set selinux to permissive in order to analyze.

您可以将selinux设置为允许,以便进行分析。

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

Selinux将记录但允许访问。你可以检查/var/log/audit/audit日志的详细信息。也许您需要更改selinux上下文。这个,你将使用chcon命令。如有需要,请出示你的审计报告。登录到更详细的答案。

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

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

    # setenforce 1

#10


1  

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 {} +");

#11


0  

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

我正在运行Ubuntu,正如上面所说的:没有人不会在Ubuntu上工作。你得到了错误:

chown: invalid group: 'nobody:nobody'

Instead you should use the 'nogroup', like:

相反,你应该使用“nogroup”,比如:

chown nobody:nogroup <dirname>

#1


45  

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

一种非常简单的方法是让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) ) {
     $oldmask = umask(0);  // helpful when used in linux server  
     mkdir ($dir, 0744);
 }

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

This saves you the hassle with permissions.

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

#2


31  

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

将目录的所有者设置为运行apache的用户。在linux上往往没有人

chown nobody:nobody <dirname>

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

这样,您的文件夹将不会是可写的,但仍然可以为apache编写:)

#3


6  

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

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

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:

这里的第一列显示用户名。现在你知道了要写文件的用户,也就是phpuser你现在可以继续设置你的php脚本要写东西的目录的权限:

sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere

#4


3  

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

可以使用PHP的chmod()更改文件夹的权限。关于如何使用该命令的更多信息如下: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.

如果您在将权限设置为777 (world writable)时出现500个错误,那么这意味着您的服务器将设置为防止执行此类文件。这是出于安全考虑。在这种情况下,您将希望使用755作为文件的最高权限。

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.

如果在执行PHP文档的文件夹中生成一个error_log文件,您将希望查看最后几个条目。这将使您了解脚本在哪里出错。

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

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

#5


2  

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

我发现,在HostGator中,您必须将文件设置为CMOD 644和文件夹到755。因为我是基于他们的技术支持做的,所以它可以和HostGator一起工作

#6


2  

Simple 3-Step Solution

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

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


Step 1: Determine PHP User

步骤1:确定PHP用户

Create a PHP file containing the following:

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

<?php echo `whoami`; ?>

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

将它上载到您的web服务器。输出应类似于以下内容:

www-data

Therefore, the PHP user is www-data.

因此,PHP用户是www-data。


Step 2: Determine Owner of Directory

步骤2:确定目录的所有者

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

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

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.

因此,目录的所有者是exampleuser1。


Step 3: Change Directory Owner to PHP User

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

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

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

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

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

验证web目录的所有者已被更改:

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.

因此,示例文件夹的所有者已经成功地更改为PHP用户:www-data。


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

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

#7


1  

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

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

#8


1  

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.

由于我不愿意将0777发送到我的php目录,所以我创建了一个tmp目录,其中包含了权利0777,在这里我创建了需要写入的文件。

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

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

#9


1  

You can set selinux to permissive in order to analyze.

您可以将selinux设置为允许,以便进行分析。

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

Selinux将记录但允许访问。你可以检查/var/log/audit/audit日志的详细信息。也许您需要更改selinux上下文。这个,你将使用chcon命令。如有需要,请出示你的审计报告。登录到更详细的答案。

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

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

    # setenforce 1

#10


1  

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 {} +");

#11


0  

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

我正在运行Ubuntu,正如上面所说的:没有人不会在Ubuntu上工作。你得到了错误:

chown: invalid group: 'nobody:nobody'

Instead you should use the 'nogroup', like:

相反,你应该使用“nogroup”,比如:

chown nobody:nogroup <dirname>