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 WordPress 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.
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.
You missed the 'recursive' flag - see Satish's answer.
is_dir() is bit faster, than file_exists()
@YuryPliashkou Yeah maybe, but it doesn’t work if there’s already a file with that name.
question here: so if there'd be a file named 'directory' in path/to , is_dir would return true, but file_exists would return false?
file_exists
— Checks whether a file or directory existsis_file
— Tells whether the filename is a regular fileis_dir
— Tells whether the filename is a directory