Anyone know what I'm doing wrong here? I'm trying to get this code to work so that if a directory exists.. the mkdir() function is bypassed (so i dont get an error) but it seems that no matter what I throw in my is_dir() function it always hops down to the else.

$user = $_POST["user"];
$dirname = "./cp/userfiles/{$user}/"; 

if (is_dir($dirname)) {    
echo "The directory {$user} exists";    
} else {    
mkdir("userfiles/{$user}", 0777);    
echo "The directory {$user} was successfully created.";    
}

Recommended Answers

All 4 Replies

Try the following:

$user = stripslashes($_POST["user"]);
$dirname = "./cp/userfiles/$user/"; 
 
if (is_dir($dirname)) {    
echo "The directory $user exists";    
} else {    
mkdir("userfiles/$user", 0777);    
echo "The directory $user was successfully created.";    
}

Nope, that didn't help. The $_post["user"] returns a user id num from my database, so that shouldn't be the problem. I'll play around with the dirname a bit to see if it's possibly going into the wrong directory.

Edit: I thought i tried editing that already, but that was the problem ^. Maybe I didn't refresh the page when I went to test it out. Thanks for the help though.

The $_post["user"] returns a user id num from my database

Then perhaps rename the array to the variable $user and change it elsewhere in the script because using the $_POST array for database retrieval could be your problem and it's insecure too as $_POST is a global array (not good).

Then perhaps rename the array to the variable $user and change it elsewhere in the script because using the $_POST array for database retrieval could be your problem and it's insecure too as $_POST is a global array (not good).

No no, that's not what I'm doing. I'm actually copying a variable called $uid into a hidden form input and then using $_POST to retrieve it from the form.

So $uid is being called from the database with a mysql_query.

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.