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.
Within WordPress there's also the very handy function wp_mkdir_p which will recursively create a directory structure.
Source for reference:-
To create a folder if it doesn't already exist
Considering the question's environment.
And quoting from: http://php.net/manual/en/function.mkdir.php
Manual says that the only required parameter is the
$pathname
!so, We can simply code:
Explanation:
We don't have to pass any parameter or check if folder exists or even pass mode parameter unless needed; for the following reasons:
mode
is ignored on Windows Hosting running PHP.mkdir
command has build in checker if folder exists; so we need to check the return only True|False ; and its not an error, its a warning only, and Warning is disabled in hosting servers by default.This is just another way to look into the question and not claiming a better or most optimal solution.
Tested on PHP7, Production Server, Linux
This is the most up-to-date solution without error suppression:
I need the same thing for a login site. I needed to create a directory with a two variables. The $directory is the main folder where I wanted to create another sub-folder with the users license number.
What about a helper function like this:
It will return
true
if the directory was successfully created or already exists, andfalse
if the directory couldn't be created.A better alternative is this (shouldn't give any warnings):