Mkdir() set permission 777

10,593

Solution 1

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. 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. 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? 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. Doing a specific chmod() call will do the trick though:

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. 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. 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. Usually the httpd_public_content_rw_t context is required on upload folders.

Solution 2

Check this post: 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. 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. You'd need to do:

<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1', 0777);
chmod('/test1/test2', 0777);
?>
Share:
10,593
John Smith
Author by

John Smith

Updated on June 28, 2022

Comments

  • John Smith
    John Smith almost 2 years

    How do I set permissions for folders below 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?