Create a folder if it doesn't already exist


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.

1/8/2016 7:00:46 PM

Accepted Answer

Try this:

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);

Note that 0777 is already the default mode for directories and may still be modified by the current umask.

7/3/2013 7:45:12 PM

Here is the missing piece. You need to pass 'recursive' flag as third argument (boolean true) in mkdir call like this:

mkdir('path/to/directory', 0755, true);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow