Hi, I can't seem to find the answer I'm looking for online (maybe keywords are too common?). Is my code only verifying whether my directory is created? Or is it actually validating? I need it to validate, but I'm unsure whether or not it's doing that.

$dirPath = $_POST['company'];  

$result = mkdir($dirPath, 0755);  
if ($result == 1) {  
    echo '<br/>'.$dirPath . " has been created".'<br/>';
} else {  
    echo '<br/>'.$dirPath . " has NOT been created".'<br/>';
}

Upon a second submission with the same company name and uploaded image, I get this error:

Warning: mkdir() [function.mkdir]: File exists in /home/***/***/***/formulaires/processForm-test.php on line 69

Line 69, is as you guessed, the other code above (minus the first line).

What am I doing wrong?

Recommended Answers

All 6 Replies

Check first with is_dir

It still gives me the same error message. Doesn't it verify whether this is a directory or not? If so, I still to validate it.

What's your new code?

$dirPath = $_POST['company']; 

if(is_dir($dirPath)) { 
    $result = mkdir($dirPath, 0755);  
    if ($result == 1) {  
        echo '<br/>'.$dirPath . " has been created".'<br/>';
    } else {  
        echo '<br/>'.$dirPath . " has NOT been created".'<br/>';
    }
} else {
   echo '<br/>'.$dirPath . " already exists".'<br/>';
}

@ pritaeas, this is the last code I use to ignore the error message.

$dirPath = $_POST['company'];  
$dirExists = is_dir($dirPath);

if(!dirExists)
    $dirExists = mkdir($dirPath, 0755);  

echo '<br/>'.'Le dossier '.$dirPath . (($dirExists)? "" : "DO NOT")." existe déja".'<br/>';
*/

// @ ignores fatal PHP error    
/*@$result = mkdir($dirPath, 0755);  
if ($result == 1) {  
    echo '<br/>'.'Le dossier '.$dirPath . " a été créer".'<br/>';
} else {  
    echo '<br/>'.'Le dossier '.$dirPath . " n'a PAS été créer".'<br/>';
}

@ pritaeas and mrvijayakumar: For the validation, I just realized that I don't need it to validate it like javascript does. But, if I wanted to, how could I make it happen? It that even possible? I usually would use js to prevent it from going to the next page, but if that fails/is hacked, php kicks in. So, could I do something similar with PHP? Where it does exactly what js would do? (In other words, prevent it from going to the next page)

@ mrvijayakumar: That works the same way as my older code. I was so eager to try to it out lol. Although now it tells me that it hasn't been created, but doesn't tell me that it already exists.

P.S.: Sorry for the late reply guys. I only have access to the code three days a week.

Upon further revision (I created a new folder). Your code does not work at all for the creation process, as it gives out an error. Thank you for trying though. For now I will use my code, and suppress the error instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.