How to Create a Folder If It Doesn’t Already Exist

by Ataul Ghani

Disclosure

Have issue with creating a folder if it doesn’t already exist? If you’ve run into a few cases with WordPress installs with Bluehost where you’ve encountered errors with your  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 you need to add code to my theme that checks for the folder and creates it otherwise. This article will show you how to create a folder if it doesn’t already exist.

Learn About WordPress Themes

Create a Folder If It Doesn’t Already Exist


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

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

Share on:

Disclosure: This post may contain affiliate links or paid partnerships. We may earn a commission if you click a link and make a purchase, at no extra cost to you. See our disclosure for more info.
Photo of author
Written by Ataul Ghani