- This topic is empty.
-
AuthorPosts
-
March 6, 2019 at 4:37 am #10810joaorodr84Participant
If you want to avoid the
file_exists
vs.is_dir
problem, I would suggest you to look here.I tried this and it only creates the directory if the directory does not exist. It does not care if there is a file with that name.
/* Creates the directory if it does not exist */ $path_to_directory = 'path/to/directory'; if (!file_exists($path_to_directory) && !is_dir($path_to_directory)) { mkdir($path_to_directory, 0777, true); }
August 24, 2019 at 5:27 am #10802nasir-khanParticipantYou first need to check if directory exists
file_exists('path_to_directory')
Then use
mkdir(path_to_directory)
to create a directorymkdir( string $pathname [, int $mode = 0777 [, bool $recursive = FALSE [, resource $context ]]] ) : bool
More about mkdir() here
Full code here:
$structure = './depth1/depth2/depth3/'; if (!file_exists($structure)) { mkdir($structure); }
May 7, 2020 at 10:00 am #10811ulidu-theerakeParticipantHere you go.
if (!is_dir('path/to/directory')) { if (!mkdir('path/to/directory', 0777, true) && !is_dir('path/to/directory')) { throw new \RuntimeException(sprintf('Directory "%s" was not created', 'path/to/directory')); } }
October 15, 2020 at 12:57 pm #10804jens-trnellParticipantAs a complement to current solutions, a utility function.
function createDir($path, $mode = 0777, $recursive = true) { if(file_exists($path)) return true; return mkdir($path, $mode, $recursive); } createDir('path/to/directory');
It returns
true
if already exists or successfully created. Else it returns false.December 2, 2020 at 2:23 am #10816wp-punkParticipantThe best way is to use the
wp_mkdir_p
function. This function will recursively create a folder with the correct permissions.Also, you can skip folder exists condition because the function returns:
true
when the directory was created or existed beforefalse
if you can’t create the directory.
Example:
$path = 'path/to/directory'; if ( wp_mkdir_p( $path ) ) { // Directory exists or was created. }
More: https://developer.wordpress.org/reference/functions/wp_mkdir_p/
February 13, 2021 at 4:35 am #10812usman-ahmedParticipantFor your specific question about WordPress, use the following code:
if (!is_dir(ABSPATH . 'wp-content/uploads')) wp_mkdir_p(ABSPATH . 'wp-content/uploads');
Function Reference: WordPress wp_mkdir_p. ABSPATH is the constant that returns WordPress working directory path.
There is another WordPress function named
wp_upload_dir()
. It returns the upload directory path and creates a folder if doesn’t already exists.$upload_path = wp_upload_dir();
The following code is for PHP in general.
if (!is_dir('path/to/directory')) mkdir('path/to/directory', 0777, true);
Function reference: PHP is_dir()
August 3, 2022 at 6:45 am #10805ashok-kumawatParticipantWe can create folder using
mkdir
. Also we can set permission for it.Value Permission 0 cannot read, write or execute 1 can only execute 2 can only write 3 can write and execute 4 can only read 5 can read and execute 6 can read and write 7 can read, write and execute
<?PHP // Making a directory with the provision // of all permissions to the owner and // the owner's user group mkdir("/documents/post/", 0770, true) ?>
-
AuthorPosts
- You must be logged in to reply to this topic.