PHP mkdir:权限被拒绝问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5246114/
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
PHP mkdir: Permission denied problem
提问by folos
I am trying to create a directory with PHP mkdir function but I get an error as follows: Warning: mkdir() [function.mkdir]: Permission denied in ...
. How to settle down the problem?
我想用PHP创建的mkdir功能的目录,但我得到一个错误如下:Warning: mkdir() [function.mkdir]: Permission denied in ...
。如何解决问题?
回答by simpleengine
I know this is an old thread, but it needs a better answer. You shouldn't need to set the permissions to 777, that is a security problem as it gives read and write access to the world. It may be that your apache user does not have read/write permissions on the directory.
我知道这是一个旧线程,但它需要一个更好的答案。您不需要将权限设置为 777,这是一个安全问题,因为它提供了对世界的读写访问权限。可能是您的 apache 用户对该目录没有读/写权限。
Here's what you do in Ubuntu
这是你在 Ubuntu 中所做的
Make sure all files are owned by the Apache group and user. In Ubuntu it is the www-datagroup and user
chown -R www-data:www-data /path/to/webserver/www
Next enabled all members of the www-data group to read and write files
chmod -R g+rw /path/to/webserver/www
确保所有文件都归 Apache 组和用户所有。在 Ubuntu 中,它是www-data组和用户
chown -R www-data:www-data /path/to/webserver/www
接下来启用 www-data 组的所有成员读写文件
chmod -R g+rw /path/to/webserver/www
The php mkdir()
function should now work without returning errors
phpmkdir()
函数现在应该可以工作而不会返回错误
回答by Chris Ingis
Late answer for people who find this via google in the future. I ran into the same problem.
对于将来通过谷歌找到这个的人的迟到答案。我遇到了同样的问题。
NOTE: I AM ON MAC OSX LION
注意:我使用的是 MAC OSX LION
What happens is that apache is being run as the user "_www" and doesn't have permissions to edit any files. You'll notice NO filesystem functions work via php.
发生的情况是 apache 以用户“_www”的身份运行并且没有编辑任何文件的权限。您会注意到没有文件系统功能通过 php 工作。
How to fix:
怎么修:
Open a finder window and from the menu bar, choose Go > Go To Folder > /private/etc/apache2
打开查找器窗口,然后从菜单栏中选择 Go > Go To Folder > /private/etc/apache2
now open httpd.conf
现在打开httpd.conf
find:
找:
User _www
Group _www
change the username:
更改用户名:
User <YOUR LOGIN USERNAME>
Now restart apache by running this form terminal:
现在通过运行此表单终端重新启动 apache:
sudo apachectl -k restart
If it still doesn't work, I happen to do the following before I did the above. Could be related.
如果它仍然不起作用,我碰巧在执行上述操作之前执行了以下操作。可能有关系。
Open terminal and run the following commands: (note, my webserver files are located at /Library/WebServer/www. Change according to your website location)
打开终端并运行以下命令:(注意,我的网络服务器文件位于/Library/WebServer/www。根据您的网站位置更改)
sudo chmod 775 /Library/WebServer/www
sudo chmod 775 /Library/WebServer/www/*
回答by Wilt
Don't set permissions to 777 when using mkdir in PHP
在 PHP 中使用 mkdir 时不要将权限设置为 777
Link only answers are not considered good practice on StackOverflow, but the advice that is given here should generally NOT be followed up.
仅链接答案在 StackOverflow 上不被视为良好实践,但通常不应遵循此处给出的建议。
I would like to revert to this great answer on a similar question. I quote:
Please stop suggesting to use 777. You're making your file writeable by everyone, which pretty much means you lose all security that the permission system was designed for. If you suggest this, think about the consequences it may have on a poorly configured webserver: it would become incredibly easy to "hack" the website, by overwriting the files. So, don't.
请停止建议使用 777。您正在使每个人都可以写入您的文件,这几乎意味着您失去了权限系统设计的所有安全性。如果您建议这样做,请考虑它可能对配置不当的网络服务器造成的后果:通过覆盖文件来“入侵”网站会变得非常容易。所以,不要。
回答by Erik
Fix the permissions of the directory you try to create a directory in.
修复您尝试在.
回答by Boris
I know that this thread is old, but perhaps this will help someone, one day.
我知道这个线程很旧,但也许有一天这会对某人有所帮助。
The problem why PHP says "Permission denied" for mkdir() - wrong url path. So, to fix it, all you need it's to obtain correct path. I did it this way:
为什么 PHP 对 mkdir() 说“权限被拒绝”的问题 - 错误的 url 路径。因此,要修复它,您只需要获得正确的路径即可。我是这样做的:
<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$dir = $root . '/somefolder/';
if( !file_exists($dir) ) {
mkdir($dir, 0755, true);
}
?>
回答by Jon Skarpeteig
You need to have file system permission to create the directory.
您需要具有文件系统权限才能创建目录。
Example: In Ubuntu 10.04 apache (php) runs as user: www-data in group: www-data
示例:在 Ubuntu 10.04 apache (php) 中以用户身份运行:www-data 组:www-data
Meaning the user www-data needs access to create the directory.
这意味着用户 www-data 需要访问权限才能创建目录。
You can try this yourself by using: 'su www-data' to become the www-data user.
您可以自己尝试使用: 'su www-data' 成为 www-data 用户。
As a quick fix, you can do: sudo chmod 777 my_parent_dir
作为快速修复,您可以执行以下操作:sudo chmod 777 my_parent_dir
回答by maxwells
What the other thing you can do is go to: /etc/sudoers
您可以做的另一件事是转到:/etc/sudoers
There add the following line which gives the permission to that user www-data ALL=(ALL:ALL) ALL Why www-data ? this is because apache is running by this user name.
添加以下行,授予该用户的权限 www-data ALL=(ALL:ALL) ALL 为什么是 www-data ?这是因为 apache 正在以此用户名运行。
Incase if your user is different then try username ALL=(ALL:ALL) ALL
如果您的用户不同,请尝试用户名 ALL=(ALL:ALL) ALL
This worked for me.
这对我有用。
回答by arungisyadi
I have this problem just now, my best solution I can give to you right now (despite that you didn't include any of your code) would be:
我刚才有这个问题,我现在可以给你的最好的解决方案(尽管你没有包含任何代码)是:
- Check how you name your destination folder, eg: new_folder (sometimes this can cause error for permission as most hosts don't allow names using underscore, dash, etc to be created at run time). It worked for me.
If you were using recursive command to create sub-folders don't forget to put 0755 (remember to include 0 at the start) to the mkdir command, eg:
if(!file_exists($output)){ if (!mkdir($output, 0755, true)) {//0755 die('Failed to create folders...'); } }
- 检查您如何命名目标文件夹,例如:new_folder(有时这会导致权限错误,因为大多数主机不允许在运行时创建使用下划线、破折号等的名称)。它对我有用。
如果您使用递归命令创建子文件夹,请不要忘记将 0755(记得在开头包含 0)添加到 mkdir 命令,例如:
if(!file_exists($output)){ if (!mkdir($output, 0755, true)) {//0755 die('Failed to create folders...'); } }
This is also worked for me just now.
这也适用于我刚才。
回答by Marcelo Agimóvel
If you'r using LINUX, first let's check apache's user, since in some distros it can be different:
如果您使用的是 LINUX,首先让我们检查 apache 的用户,因为在某些发行版中它可能有所不同:
egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
Let's say the result is "http". Do the folowwing, just remember change path_to_folder to folders path:
假设结果是“http”。执行以下操作,只需记住将 path_to_folder 更改为文件夹路径:
chown -R http:http path_to_folder
chmod -R g+rw path_to_folder
回答by ceph3us
check if its not a issue with umask
检查它是否不是 umask 的问题
if (!function_exists('mkdir_r')) {
/**
* create directory recursively
* @param $dirName
* @param int $rights
* @param string $dir_separator
* @return bool
*/
function mkdir_r($dirName, $rights = 0744, $dir_separator = DIRECTORY_SEPARATOR) {
$dirs = explode($dir_separator, $dirName);
$dir = '';
$created = false;
foreach ($dirs as $part) {
$dir .= $part . $dir_separator;
if (!is_dir($dir) && strlen($dir) > 0) {
$created = mkdir($dir, $rights);
}
}
return $created;
}
}
if (!function_exists('ensure_dir')) {
/**
* ensure directory exist if not create
* @param $dir_path
* @param int $mode
* @param bool $use_mask
* @param int $mask
* @return bool
*/
function ensure_dir($dir_path, $mode = 0744, $use_mask = true, $mask = 0002) {
// set mask
$old_mask = $use_mask && $mask != null
? umask($mask)
: null;
try {
return is_dir($dir_path) || mkdir_r($dir_path, $mode);
} finally {
if ($use_mask && $old_mask != null) {
// restore original
umask($old_mask);
}
}
}
}