简体   繁体   English

"PHP mkdir:权限被拒绝问题"

[英]PHP mkdir: Permission denied problem

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 ...<\/code> .我正在尝试使用 PHP mkdir 函数创建一个目录,但出现如下错误: Warning: mkdir() [function.mkdir]: Permission denied in ...<\/code> 。 How to settle down the problem?如何解决问题?

"

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.您不需要将权限设置为 777,这是一个安全问题,因为它提供了对世界的读写访问权限。 It may be that your apache user does not have read/write permissions on the directory.可能是您的 apache 用户对该目录没有读/写权限。

Here's what you do in Ubuntu这是你在 Ubuntu 中所做的

  1. Make sure all files are owned by the Apache group and user.确保所有文件都归 Apache 组和用户所有。 In Ubuntu it is the www-data group and user在 Ubuntu 中,它是www-data组和用户

    sudo chown -R www-data:www-data /path/to/webserver/www

  2. Next enabled all members of the www-data group to read and write files接下来启用 www-data 组的所有成员读写文件

    sudo chmod -R g+rw /path/to/webserver/www

The php mkdir() function should now work without returning errors php mkdir()函数现在应该可以正常工作而不会返回错误

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.发生的情况是 apache 以用户“_www”的身份运行并且没有编辑任何文件的权限。 You'll notice NO filesystem functions work via php.您会注意到没有文件系统功能通过 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/*

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.请停止建议使用 777。您正在使每个人都可以写入您的文件,这几乎意味着您失去了权限系统设计的所有安全性。 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.所以,不要。

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.为什么 PHP 对 mkdir() 说“权限被拒绝”的问题 - 错误的 url 路径。 So, to fix it, all you need it's to obtain correct path.因此,要修复它,您只需要获得正确的路径即可。 I did it this way:我是这样做的:

<?php

$root = $_SERVER["DOCUMENT_ROOT"];
$dir = $root . '/somefolder/';

if( !file_exists($dir) ) {
    mkdir($dir, 0755, true);
}

?>

修复您尝试.

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

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:我刚才有这个问题,我现在可以给你的最好的解决方案(尽管你没有包含任何代码)是:

  1. 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).检查您如何命名目标文件夹,例如:new_folder(有时这会导致权限错误,因为大多数主机不允许在运行时创建使用下划线、破折号等的名称)。 It worked for me.它对我有用。
  2. 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:如果您使用递归命令创建子文件夹,请不要忘记将 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.这也适用于我刚才。

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 ?添加以下行,授予该用户的权限 www-data ALL=(ALL:ALL) ALL 为什么是 www-data ? this is because apache is running by this user name.这是因为 apache 正在以此用户名运行。

Incase if your user is different then try username ALL=(ALL:ALL) ALL如果您的用户不同,请尝试用户名 ALL=(ALL:ALL) ALL

This worked for me.这对我有用。

Since you are on a mac, you could add yourself to the _www (the apache user group) group on your mac:由于您使用的是 mac,您可以将自己添加到 mac 上的 _www(apache 用户组)组:

sudo dseditgroup -o edit -a $USER -t user _www

and add _www user to the wheel group which seems to be what the mac creates files as:并将_www用户添加到wheel组,这似乎是mac创建文件的内容:

sudo dseditgroup -o edit -a _www -t user wheel

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".假设结果是“http”。 Do the folowwing, just remember change path_to_folder to folders path:执行以下操作,只需记住将 path_to_folder 更改为文件夹路径:

chown -R http:http path_to_folder
chmod -R g+rw path_to_folder

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

After you install the ftp server with sudo apt-get install vsftpd you will have to configure it.使用sudo apt-get install vsftpd安装 ftp 服务器后,您必须对其进行配置。 To enable write access you have to edit the /etc/vsftpd.conf file and uncomment the要启用写访问,您必须编辑/etc/vsftpd.conf文件并取消注释

#write_enable=YES

line, so it should read行,所以它应该读

write_enable=YES

Save the file and restart vsftpd with sudo service vsftpd restart .保存文件并重新启动vsftpd使用sudo service vsftpd restart

For other configuration options consult this documentation or man vsftpd.conf对于其他配置选项,请参阅此文档man vsftpd.conf

This error occurs if you are using the wrong path.如果您使用了错误的路径,则会发生此错误。

For eg:例如:

I'm using ubuntu system and my project folder name is 'myproject'我正在使用 ubuntu 系统,我的项目文件夹名称是“myproject”

Suppose I have myproject/public/report directory exist and want to create a new folder called orders假设我有myproject/public/report目录,并且想要创建一个名为orders的新文件夹

Then I need a whole path where my project exists.然后我需要我的项目所在的完整路径。 You can get the path by PWD command可以通过 PWD 命令获取路径

So my root path will be = '/home/htdocs/myproject/'所以我的根路径将是 = '/home/htdocs/myproject/'

$dir = '/home/htdocs/myproject/public/report/Orders'; $dir = '/home/htdocs/myproject/public/report/Orders';

if (!is_dir($dir)) {
   mkdir($dir, 0775, true);
}

It will work for you !!它会为你工作!! Enjoy !!享受 !!

I did all I could to fix this issue and none of the above answers helped.我尽我所能来解决这个问题,但上述答案都没有帮助。 Finally restarting apache did the trick最后重新启动 apache 就成功了

sudo service apache2 restart<\/code>

My system is running Ubuntu 20 with Samba sharing the directories that had the permission issue.我的系统运行 Ubuntu 20,Samba 共享存在权限问题的目录。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM