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.
Something a bit more universal since this comes up on google. While the details are more specific, the title of this question is more universal.
This will take a path, possibly with a long chain of uncreated directories, and keep going up one directory until it gets to an existing directory. Then it will attempt to create the next directory in that directory, and continue till it's created all the directories. It returns true if successful.
Could be improved by providing a stopping level so it just fails if it goes beyond user folder or something and by including permissions.
Faster way to create folder:
Try this:
Note that
0777
is already the default mode for directories and may still be modified by the current umask.Recursively create directory path:
Inspired by Python's
os.makedirs()
Here is the missing piece. You need to pass 'recursive' flag as third argument (boolean true) in mkdir call like this: