简体   繁体   English

Mkdir()设置权限777

[英]Mkdir() set permission 777

How do I set permissions for folders below 777? 如何设置777以下文件夹的权限?

 mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0777, true);
 mkdir(getcwd() . '/public/profile/usr'.$user->getId() . '/patients', 0777, true);
 mkdir(getcwd() . '/public/profile/usr'.$user->getId() . '/picture', 0777, true);

Folder permissions for these are the type below: 这些文件夹的权限为以下类型:

drwxr-xr-x

And I want it set to: 我希望将其设置为:

drwxrwxrwx

Can you tell me please how to solve this problem? 你能告诉我如何解决这个问题吗?

First off, there is never a good reason to set folder or file permissions to 777. People that feel the need to do this usually either are blindly following a (bad) online tutorial or have an underlying security/permissions problem that they are unable to fix. 首先, 永远没有充分的理由将文件夹或文件权限设置为777。感到需要这样做的人们通常要么盲目地关注(糟糕的)在线教程,要么遇到无法解决的潜在安全性/权限问题。固定。 Usually any uploading/writing problems can be fixed by setting the proper owner/group on the folder (usually the www-data or apache user), 777 means that anybody can write or execute anything in those folders. 通常,可以通过在文件夹(通常是www-data或apache用户)上设置适当的所有者/组来解决任何上载/写入问题。777表示任何人都可以在这些文件夹中写入或执行任何操作。 You're basically leaving your back door open and unlocked in a bad neighbourhood. 基本上,您是在一个不好的邻居中打开后门并解锁。 Also see How will a server become vulnerable with chmod 777? 另请参阅chmod 777如何使服务器容易受到攻击? for further details. 有关更多详细信息。

That being said, I've seen problems like this with mkdir() before, it is usually because of a certain umask on the system being set, forcing newly created folders to have 755 permissions. 话虽如此,我以前在mkdir()已经遇到过类似的问题,通常是由于设置了系统上的某个umask ,迫使新创建的文件夹具有755权限。 Doing a specific chmod() call will do the trick though: 进行特定的chmod()调用可以解决问题:

mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0777, true);
chmod(getcwd() . '/public/profile/usr'.$user->getId(), 777);
// And so on...

But, the better solution : fix your permissions/security issue. 但是, 更好的解决方案 :解决权限/安全性问题。 Just set the proper owner (the process that PHP is running under), this will avoid having to set 777 permissions, but will allow proper upload handling. 只需设置适当的所有者(运行PHP的进程),就可以避免设置777权限,但可以进行正确的上传处理。 This should already be the default, but you can force it with: 这应该已经是默认设置,但是您可以使用以下命令强制使用它:

// This will get the user the PHP process is running under on Linux machines:
$user = `whoami`;

mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0755, true);
chown(getcwd() . '/public/profile/usr'.$user->getId(), $user);
// And so on

This will set 755 permissions on the folder and set ownership to the PHP process, meaning that PHP has full permissions on the folder, but any other users do not. 这将在文件夹上设置755权限,并将所有权设置为PHP进程,这意味着PHP对文件夹具有完全权限,而其他任何用户则没有。 This is more secure, yet no less convenient. 这样更安全,但同样方便。

The problem might also be caused by SELinux , in which case chmod 777 is not going to help you much anyway. 该问题也可能是由SELinux引起的,在这种情况下,chmod 777不会对您有太大帮助。 Usually the httpd_public_content_rw_t context is required on upload folders. 通常,上传文件夹上需要httpd_public_content_rw_t上下文。

Check this post: http://php.net/manual/de/function.mkdir.php 检查这篇文章: http : //php.net/manual/de/function.mkdir.php

Answer from there... 从那里回答...

When using the recursive parameter bear in mind that if you're using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. 使用递归参数时,请记住,如果在mkdir()之后使用chmod()来设置模式,而无需通过uchar()的值对其进行修改,则需要在所有创建的目录上调用chmod()。 ie: 即:

<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1/test2', 0777);
?> 

May result in "/test1/test2" having a mode of 0777 but "/test1" still having a mode of 0755 from the mkdir() call. 可能导致mkdir()调用中的“ / test1 / test2”的模式为0777,但“ / test1”的模式仍然为0755。 You'd need to do: 您需要执行以下操作:

<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1', 0777);
chmod('/test1/test2', 0777);
?>

Here is one of the many possible ways of doing it: 这是执行此操作的许多可能方法之一:

    <?php
        $baseDir    = getcwd() . '/public/profile/usr'. $user->getId();
        $arrFolders = array('/patients', '/picture');

        // FIRST chmod THE BASE DIRECTORY TO HAVE THE PERMISSIONS YOU WANT:
        chmod($baseDir, 0777);

        // LOOP THROUGH THE ARRAY OF FOLDERS YOU WANT TO CREATE & CREATE THEM WITH APPROPRIATE PERMISSIONS:         
        foreach($arrFolders as $folder){
            try{
                mkdir($baseDir . $folder, 0777, true);
            }catch(Exception $e){
                // HANDLE ANY EXCEPTION SHOULD ONE OCCUR...
            }
        }

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

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