First of all, you should adhere to the absolute patches when working with FileSystem, and also there are two minor flaws:
is_dir()
- Checks whether file exists and its a directory. Thereforefile_exists()
is kinda redundant.If you work with the same string anywhere else, it would be better to save its value in a variable.
And finally, your code should look like this,
$target = dirname(__FILE__) . '/hellodir';
if (is_dir($target)) {
echo "folder already exists";
} else {
echo "no folder, creating";
// The 3-rd bool param includes recursion
mkdir($target, 0777, true);
}
This will work as expected.