问题描述
以下PHP脚本无法创建目录.如果目录已经存在,它也将无法创建文件.
The following PHP script fails to create the directory. It will also fail to create the file (when the directory already exists).
ini_set('error_reporting', E_ALL);
define('ABSPATH', $_SERVER['DOCUMENT_ROOT']);
echo ABSPATH . '<br /><br />';
$dir_to_make = ABSPATH . '/aaatest';
$file_to_make = ABSPATH . '/aaatest/aaatest.txt';
echo umask() . '<br />';
mkdir($dir_to_make) or die('could not create directory');
fopen($file_to_make) or die('could not open/create file');
umask()返回值为18.文档根目录中有一个句点(/var/www/blah/websitename.com/httpdocs).
The umask() is returning a value of 18. The document root has a period in it ( /var/www/blah/websitename.com/httpdocs ).
我不完全了解umask(),也不确定如何正确使用它.我不知道这是否是问题,但这似乎是有可能的.我应该更改umask,创建文件/目录,然后再将其更改回吗?更改/制作/编辑文件/目录的umask应该是什么?服务器的配置应该不同吗?
I don't completely understand umask(), nor am I sure of how to properly use it. I don't know if that's the problem or not, but it does seem to be likely. Should I change the umask, create the file/directory, then change it back? What should the umask be to change/make/edit files/directories? Should the server be configured differently?
推荐答案
为了在文档根目录中创建文件,您的PHP进程必须具有写入目录的权限.通常(但不总是),PHP以与Web服务器相同的用户身份运行.该用户的名称将随不同的系统而变化.在Ubuntu和Debian上,该用户称为www-data
,在其他系统上,该用户可能只是www
,apache
或apache2
.在某些系统上,它可能是root
.
In order to create a file within the document root, your PHP process must have permissions to write to the directory. Usually (but not always) PHP runs as the same user that the web server runs as. The name of this user will vary with different systems. On Ubuntu and Debian, the user is called www-data
, on other systems it may be just www
, or apache
, or apache2
. On some systems, it may be root
.
您可以通过检查服务器超全局变量$_SERVER['USER']
的值来找出您的PHP运行的用户. phpinfo()
提供了一种简单的方法来查看此类内容.通常,PHP用户与Web服务器用户相同(但并非总是如此).
You can find out what user your PHP runs as by examining the value of the server superglobal: $_SERVER['USER']
. phpinfo()
provides an easy way look at stuff like this. Usually, the PHP user is the same as the web server user (but not always).
设置目录所有权和权限完全是另一个主题-取决于您所使用的操作系统,对服务器的访问和权限以及许多其他内容.如果您需要有关此的指示,则可以从serverfault.com开始.
setting directory ownership and permissions is another topic entirely - depends on what operating system you're on, what access and permissions you have for the server, and lots of other stuff. If you need pointers on this, you might start at serverfault.com.
祝你好运.
[edit]好,如果您以apache
身份运行,并且试图在/var/www/blah/mydomain.com/htdocs/
...中创建新目录,那么在运行时:
[edit] OK, if you're runing as apache
, and you're trying to create your new directory in /var/www/blah/mydomain.com/htdocs/
... then when you run:
> ls -splad /var/www/blah/mydomain.com/htdocs
您希望看到类似的东西:
you'd expect to see something like:
4 drwxr-xr-x 2 apache apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/
有两个有趣的部分:
drwxr-xr-x
表示:d
=目录; rwx
=用户具有Read,Write,eXecute; r-x
=组中只有Read和eXecute; r-x
=每个人都只有Read和eXecute.
drwxr-xr-x
means: d
= directory; rwx
= user has Read,Write,eXecute; r-x
= group has only Read, and eXecute; r-x
= everyone has only Read, and eXecute.
和apache apache
-第一个是拥有文件/目录的用户的名称,第二个是拥有文件/目录的组的名称.
and apache apache
- the first one is the name of the user that owns the file/directory, the second one is the name of the group that owns the file/directory.
所以,如果您看到这样的内容:
so if you saw something like this:
4 drwxr-xr-x 2 root apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/
它将不起作用,因为该目录归root
拥有(不是apache),即使该目录已被apache
分组,该目录也不是可组写的,因此该目录不能切.在这种情况下,您只需添加组写权限(chmod g+w /var/www/blah/mydomain.com/htdocs
),就可以了.
it would not work because the directory is owned by root
(not apache), and even though it's grouped by apache
, the directory isn't group-writeable so that doesn't cut it. In this scenario, you could simply add group write perms (chmod g+w /var/www/blah/mydomain.com/htdocs
), and you're good to go.
您可能会看到的其他东西是
Something else you might see is:
4 drw-r-xr-x 2 apache apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/
在这种情况下,所有权可以确定,但是目录不能由其所有者写入.您可以通过添加所有者chmod u+w /var/www/blah/mydomain.com/htdocs
的写权限来解决此问题.
In this case, the ownership is ok, but the directory isn't writeable by its owner. You can fix this by adding write permission for the owner chmod u+w /var/www/blah/mydomain.com/htdocs
.
还有许多其他变体,但这也许会有所帮助.
there are lots of other variations, but maybe this will help.
这篇关于PHP mkdir()和fopen()不起作用-权限问题? umask问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!