Create a folder if it doesn’t already exist
I have installed wordpress in bluehost server and after installing wordpress in it i have encountered some cases where i faced errors with my wordpress theme because even after complete installation of wordpress the upload folder of wordpress ( wp-content/uploads ) did not exist.
Apparently the Bluehost company’s cPanel WordPress installer doesn’t create this folder, and I haven’t had such a problem on any other company’s server yet. So I need to add code to my theme that checks the folder.
Friends, if you are also facing this problem, then do not panic, we are going to give you complete information in this article and I am going to give many solutions to it.
Solution 1:
First try this solution using mkdir and if still it is not right then try another solution.
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
- dir=/home/dir_name if [ ! – d $dir ] then mkdir $dir else echo “Directory exists” fi.
- You can directory use mkdir with -p option to create a directory. It will check if the directory is not available it will. mkdir -p $dir.
Note: that 0777 is already the default mode for directories and may still be modified by the current umask. the 0777 permissions allow everyone to access/read/write in the directory. and what we want for upload directory in the web server. $path_to_directory = 'path/to/directory'; if (!file_exists($path_to_directory) && !is_dir($path_to_directory)) { mkdir($path_to_directory, 0644, true); } 0644 is the good and right permissions for uploads the directory in your wordpress. because we do not usually want uploaded on the server to be executed there. true is the third parameter for setting recursive property to true or false. It allows to the creation of nested directories specified in the pathname.
Solution 2:
function makeDir($path)
{
$ret = mkdir($path); // use @mkdir if you want to suppress warnings/errors
return $ret === true || is_dir($path);
}
Returns true if this directory was created successfully or already exists, and the directory cannot be created. This is a better option (shouldn’t give any warnings):
Also Read: Best premium WordPress themes for blogs
function makeDir($path)
{
return is_dir($path) || mkdir($path);
}
For better use of this one must use wp_mkdir_p function. And this function will recursively create a folder with the correct permissions which will solve your problem. Also, you can leave the folder Exists status as it will be checked before creating.
Full source for reference
function wp_mkdir_p( $target ) {
$wrapper = null;
// strip the protocol
if( wp_is_stream( $target ) ) {
list( $wrapper, $target ) = explode( '://', $target, 2 );
}
// from php.net/mkdir user contributed notes
$target = str_replace( '//', '/', $target );
// put the wrapper back on the target
if( $wrapper !== null ) {
$target = $wrapper . '://' . $target;
}
// safe mode fails with a trailing slash under certain PHP versions.
$target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
if ( empty($target) )
$target = '/';
if ( file_exists( $target ) )
return @is_dir( $target );
// We need to find the permissions of the parent folder that exists and inherit that.
$target_parent = dirname( $target );
while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
$target_parent = dirname( $target_parent );
}
// Get the permission bits.
if ( $stat = @stat( $target_parent ) ) {
$dir_perms = $stat['mode'] & 0007777;
} else {
$dir_perms = 0777;
}
if ( @mkdir( $target, $dir_perms, true ) ) {
// If a umask is set that modifies $dir_perms, we'll have to re-set the $dir_perms correctly with chmod()
if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
for ( $i = 1; $i <= count( $folder_parts ); $i++ ) {
@chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
}
}
return true;
}
return false;
}
Read More: What Is WordPress, Used For?
Also Read: How to make contact us page in WordPress