Search code examples
phpformsfile-uploadmkdir

mkdir - create a subdirectory with specific name


After hours of searching here and Google, I decided to ask for help. I want the code posted to create a subdirectory in directory uploads, named by the variable $userDir. It does not give any errors in php error logs but it also does not create the sub-directory.

Code:

    <?php

    $userDir = $_POST['user_dir'];
    $targetFolder = '/uploads';
    if (!file_exists($targetFolder.'/'.$userDir)) {
   mkdir($targetFolder.'/'.$userDir, 0700, true);  
}
   //* Some other code here
    ?>

Does anyone know why it does not create the folder?


Solution

  • Then the folder may already exist - but not where you think it is.

    Try replacing your mkdir line to:

    if (!file_exists(getcwd().$targetFolder.'/'.$userDir)) {
       mkdir(getcwd().$targetFolder.'/'.$userDir, 0700, true);  
    }
    

    Correct your target path :

     $targetPath = $_SERVER['DOCUMENT_ROOT'].$targetFolder.'/'.$userDir;