Forum Moderators: coopster

Message Too Old, No Replies

mkdir error message

Checking if directory exists, and making it if it doesn't

         

jspeed

4:30 pm on May 24, 2010 (gmt 0)

10+ Year Member



I have found several posts about this, and tried several things. Nothing seems to work.

php version: 5.2.9
Apache version: 2.2.11 (Unix)

Trying to check if a directory exists, and if it doesn't, then creating it. Seems simple enough.

Script:
$category =@$_POST['category'];

if(!file_exists($category) && !is_dir($category))
{
mkdir("../../images/$category");
echo "made it";
}
else
{
echo "already exists";
}

The problem is not the creating a directory that doesn't exist, it's if the directory DOES exist, then I get this error:

Warning: mkdir() [function.mkdir]: File exists in /home/account/public_html/beta/admin/upload/browse.php on line 9
made it

Line 9 is mkdir("../../images/$category");

Any help appreciated, thanks.

coopster

4:45 pm on May 24, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to add the relative directory path (../../) to your "if" logic

Also, you should scrub that user-supplied data to be certain there are no malicious characters in the category variable.

jspeed

5:46 pm on May 24, 2010 (gmt 0)

10+ Year Member



I don't know why I didn't think of that. Thanks for the help, and the tip.

rocknbil

9:09 pm on May 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The toothpick ../../../../ syndrome has been noted to drive programmers to the brink of insanity on large file systems. :-)


$category =(isset($_POST['category']) and preg_match('/^\w+$/',$_POST['category']))?$_POST['category']:null;
// Or \d+ if numeric
if ($category) {
$url = "/images/$category";
$path = $_SERVER['DOCUMENT_ROOT'] . $url;
//
if (!is_dir($path)){ mkdir($path); }
}
else { echo "Invalid category"; }

echo "Server path for moving, writing, deleting is $path, url you would use in output is $url";

Matthew1980

9:38 pm on May 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

One thing has always confused me, and this is a prime example:-

$category = @$_POST['category'];

Why suppress the errors in an instance like this? surely you would prefer it if you knew there was something wrong.. I always avoid doing this just so that I can eradicate anything erroneous from the script. just my opinion there, please enlighten me if I have missed "a day in class" somewhere :)

And yes, I totally echo Rocknbill's thoughts on toothpick syndrome, one method I like to use it this:-

define('SERVER_PATH', dirname(__FILE__). "/"); //current dir..

returns the entire path (define it in the index file in the root of your website ), including the trailing "/", I just think it's handy to put it in a constant, then it's available throughout the scope of your script.

Cheers,
MRb

jspeed

3:59 am on May 25, 2010 (gmt 0)

10+ Year Member



Thanks for the tips. I will try them out.

@Matthew - I normally do not use the @$_POST, I had just been debugging the code trying to get it to work.

I appreciate the responses from everyone, this forum is always very helpful.