Don't create directory if it already exists in php

I use the following to create a directory on an intranet application for each client. The problem is, if the directory already exists, I get the error:

Warning: mkdir() [function.mkdir]: File exists in     C:\server2go\server2go\htdocs\customermgr\administrator\components\com_chronoforms\form_actions\custo    m_code\custom_code.php(18) : eval()'d code on line 11
 Failed to create directory...

      

Is it possible that the script does not create the directory if it already exists? Or that or does not show an error ?.

    <?php
    $customerID = $_GET['cfid'];

    /* wherever this particular script will be installed, I want to create a subfolder */ 

    /* Step 1. I need to know the absolute path to where I am now, ie where this script is running from...*/ 
    $thisdir = getcwd(); 

    /* Step 2. From this folder, I want to create a subfolder called "myfiles".  Also, I want to try and make this folder world-writable (CHMOD 0777). Tell me if success or failure... */ 

    if(mkdir($thisdir ."/customer-files/$customerID" , 0777)) 
    { 
       echo "Directory has been created successfully..."; 
    } 
    else 
    { 
       echo "Failed to create directory..."; 


    } 

    ?>

      

EDIT → → → → → →>

I tried the following but still no joy: - (

            <?php
            $customerID = $_GET['cfid'];
            $directory = "/customer-files/$customerID";
            if(file_exists($directory) && is_dir($directory)) { 
            }
            else {
            /* wherever this particular script will be installed, I want to create a subfolder */ 

            /* Step 1. I need to know the absolute path to where I am now, ie where this script is running from...*/ 
            $thisdir = getcwd(); 

            /* Step 2. From this folder, I want to create a subfolder called "myfiles".  Also, I want to try and make this folder world-writable (CHMOD 0777). Tell me if success or failure... */ 

            if(mkdir($thisdir ."/customer-files/$customerID" , 0777)) 
            { 
               echo "Directory has been created successfully..."; 
            } 
            else 
            { 
               echo "Failed to create directory..."; 


            } }

            ?>

      

+3


source to share


2 answers


Just use is_dir

and / or file_exists

and only call mkdir

if they return false.

[edit]



Wait, I just noticed eval

in your error message?

+3


source


You can use is_dir

with file_exists

to find out if a directory exists:



if(file_exists($dirname) && is_dir($dirname)) {

      

+2


source







All Articles