tags:

views:

61

answers:

2

I've run into a few cases with wordpress installs with bluehost where I've encountered errors with my wordpress theme because the uploads folder (wp-content/uploads) was not present.

Apparently the bluehost cpanel WP installer does not create this folder, though HostGator does.

So I need to add code to my theme that checks for the folder and creates it otherwise.

+3  A: 

Try this:

if (!is_dir('path/to/directory')) {
    mkdir('path/to/directory');
}
Gumbo
Looks like we have a winner! Thanks Gumbo!
Scott B
+1  A: 

What about a helper function like this:

function makeDir($path)
{
   $ret = mkdir($path); // use @mkdir if you want to suppress warnings/errors
   return $ret === true || is_dir($path);
}

It will return true if the directory was successfully created or already exists, and false if the directory couldn't be created.

Another alternative is:

function makeDir($path)
{
   return is_dir($path) || mkdir($path);
}
AndiDog
If you remove the `@` and replace it by a proper `is_dir` check, my upvote is yours :) Bonus points for checking whether the parent directory `is_writable()` for a watertight helper function.
Pekka
Using @ to suppress the errors is a performance hit. Better to check it doesn't already exist like Gumbo
Simon
Okay, removed the error suppression.
AndiDog
Regardless of error suppression, I'm inclined to -1 for the first example. The second is so much better that the first is pointless.
Justin Johnson