Search code examples
phplaravelpermissionsmkdir

Laravel 5: mkdir/Filesystem::makeDirectory with permissions from config


I'm running into an issue this morning with FileSystem::makeDirectory which directly calls mkdir. I'm trying to create a directory recursively pulling the desired mode out of my config like so:

$filesystem->makeDirectory($path, config('permissions.directory'), true);

config/permissions.php

<?php

return [
    'directory' => env('PERMISSIONS_DIRECTORY', 0755),
    'file' => env('PERMISSIONS_FILE', 0644)
];

.env

...
PERMISSIONS_DIRECTORY=0775
PERMISSIONS_FILE=0664    

When this is called, the directory is created but the permissions it gets are messed up. It gets something along the lines of dr----Sr-t+. After some research I've come to the conclusion that when I'm passing the value to the mode parameter from my config using config('permissions.directory') the mode is being treated as decimal rather than an octal. So the call to config is likely returning 775 which is being passed into the function, rather than 0775.

If I remove the call to config, the directory is created with the correct permissions:

    $filesystem->makeDirectory($path, 0775, true);

Does anyone have any idea how to get around this while still being able to store my permissions on my config file?


Solution

  • It does not work because permissions should be in octal, not in decimal. When you type 0755 as number - it is in octal format. When you are trying to use string "0755" - it will be auto converted to decimal 755. And 755 != 0755.

    So, for correctly converting of string to octal number, you should use the intval function:

    $permissions = intval( config('permissions.directory'), 8 );
    $filesystem->makeDirectory($path, $permissions, true);
    

    http://php.net/manual/en/function.intval.php