Search code examples
phpdirectorymkdir

PHP mkdir(); not working


I've been trying the function Mkdir that will be usefull in the project i'm working on. I've tried the simplest code possible but I can't get it to create the folder I want.

I've tried to changes my folder permissions, but that doesn't change (Nor 755 or 777) and the code keeps returning a fail.

Please have a look at my code :

<?php 
if(!mkdir($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777, true))
    {
        echo("echec");
    }
chmod($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777);
?>

The parent folder is "admin" and it permissions are set to 755.

Do you have any clue why this isn't working ?

EDIT : I remade it and it worked, no clue what the problem was.


Solution

  • Code

    mkdir('/2017', 0777, true)
    

    creates folder 2017 is a root folder of a file system.

    Always set ethier full path to your folder, e.g.:

    mkdir($_SERVER['DOCUMENT_ROOT'] . '/2017', 0777, true);
    // or
    mkdir('/var/www/mysite/2017', 0777, true);
    

    Or use . or .. to define proper location:

    // folder will be created in a same directory 
    // as a script which executes this code
    mkdir('./2017', 0777, true);
    
    // folder will be created in a directory up one level
    // than a script which executes this code
    mkdir('../2017', 0777, true);
    

    So, in your case it is obviously:

    mkdir($_SERVER['DOCUMENT_ROOT'] . '/admin/2017', 0777, true);