Related
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, using mkdir:
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.
Here is the missing piece. You need to pass 'recursive' flag as third argument (boolean true) in mkdir call like this:
mkdir('path/to/directory', 0755, true);
Here is something a bit more universal since this comes up on Google. While the details are more specific, the title of this question is more universal.
/**
* recursively create a long directory path
*/
function createPath($path) {
if (is_dir($path))
return true;
$prev_path = substr($path, 0, strrpos($path, '/', -2) + 1 );
$return = createPath($prev_path);
return ($return && is_writable($prev_path)) ? mkdir($path) : false;
}
This will take a path, possibly with a long chain of uncreated directories, and keep going up one directory until it gets to an existing directory. Then it will attempt to create the next directory in that directory, and continue till it's created all the directories. It returns true if successful.
It could be improved by providing a stopping level so it just fails if it goes beyond the user folder or something and by including permissions.
Use a helper function like this:
function makeDir($path)
{
$ret = mkdir($path); // use #mkdir if you want to suppress warnings/errors
return $ret === true || is_dir($path);
}
It will return true if the directory was successfully created or already exists, and false if the directory couldn't be created.
A better alternative is this (shouldn't give any warnings):
function makeDir($path)
{
return is_dir($path) || mkdir($path);
}
A faster way to create a folder:
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
Recursively create the directory path:
function makedirs($dirpath, $mode=0777) {
return is_dir($dirpath) || mkdir($dirpath, $mode, true);
}
Inspired by Python's os.makedirs()
The 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 before
false 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/
Within WordPress, there's also the very handy function wp_mkdir_p which will recursively create a directory structure.
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;
}
I needed the same thing for a login site. I needed to create a directory with two variables.
The $directory is the main folder where I wanted to create another sub-folder with the users license number.
include_once("../include/session.php");
$lnum = $session->lnum; // Users license number from sessions
$directory = uploaded_labels; // Name of directory that folder is being created in
if (!file_exists($directory . "/" . $lnum)) {
mkdir($directory . "/" . $lnum, 0777, true);
}
This is the most up-to-date solution without error suppression:
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory');
}
For 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()
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/newfolder';
if (! is_dir($upload_dir)) {
mkdir( $upload_dir, 0700 );
}
Here 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'));
}
}
You can try also:
$dirpath = "path/to/dir";
$mode = "0764";
is_dir($dirpath) || mkdir($dirpath, $mode, true);
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);
}
To create a folder if it doesn't already exist
Considering the question's environment.
WordPress.
Webhosting server.
Assuming it's Linux, not Windows running PHP.
And quoting from: mkdir
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive =
FALSE [, resource $context ]]] )
The manual says that the only required parameter is the $pathname!
So, we can simply code:
<?php
error_reporting(0);
if(!mkdir('wp-content/uploads')){
// Todo
}
?>
Explanation:
We don't have to pass any parameter or check if the folder exists or even pass the mode parameter unless needed; for the following reasons:
The command will create the folder with 0755 permission (the shared hosting folder's default permission) or 0777, the command's default.
mode is ignored on Windows hosting running PHP.
Already the mkdir command has a built-in checker for if the folder exists; so we need to check the return only True|False ; and it’s not an error; it’s a warning only, and Warning is disabled on the hosting servers by default.
As per speed, this is faster if warning disabled.
This is just another way to look into the question and not claiming a better or most optimal solution.
It was tested on PHP 7, production server, and Linux
if (!is_dir('path_directory')) {
#mkdir('path_directory');
}
We 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)
?>
As 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.
We should always modularise our code and I've written the same check it below...
We first check the directory. If the directory is absent, we create the directory.
$boolDirPresents = $this->CheckDir($DirectoryName);
if (!$boolDirPresents) {
$boolCreateDirectory = $this->CreateDirectory($DirectoryName);
if ($boolCreateDirectory) {
echo "Created successfully";
}
}
function CheckDir($DirName) {
if (file_exists($DirName)) {
echo "Dir Exists<br>";
return true;
} else {
echo "Dir Not Absent<br>";
return false;
}
}
function CreateDirectory($DirName) {
if (mkdir($DirName, 0777)) {
return true;
} else {
return false;
}
}
You first need to check if directory exists file_exists('path_to_directory')
Then use mkdir(path_to_directory) to create a directory
mkdir( 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);
}
I am new to php and trying to learn how to navigate a local file structure in for the format:
-Folder
-SubFolder
-SubSubFolder
-SubSubFolder
-SubFolder
-SubSubFolder
...
From another stackoverflow question I have been able to use this code using scandir():
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
echo $str;
}
}
?>
This allows me to generate a list of strings of all the 'SubFolder' in my folder directory.
What I am trying to do is list all the 'SubSubFolder' in each 'SubFolder', so that I can create a string of the 'SubSubFolder' name in combination with its 'SubFolder' parent and add it to an array.
<?php
$scan = scandir('Folder');
foreach($scan as $file)
{
if (!is_dir($file))
{
$str = "Folder/".$file;
//echo $str;
$scan2 = scandir($str);
foreach($scan2 as $file){
if (!is_dir($file))
{
echo "Folder/SubFolder/".$file;
}
}
}
}
?>
This however isn't working, and I wasn't sure if it was because I cannot do consecutive scandir() or if I cannot use $file again.
There is probably a better solution, but hopefully the following will be of some help.
<?php
function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = #opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces -$file</strong><br />";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
//To list folders names only and not the files within comment out the following line.
echo "$spaces $file<br />.";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
getDirectory( "folder" );
// Get the current directory
?>
I am currently trying to make a script that will find images with *.jpg / *.png extensions in directories and subdirectories.
If some picture with one of these extensions is found, then save it to an array with path, name, size, height and width.
So far I have this piece of code, which will find all files, but I don't know how to get only jpg / png images.
class ImageCheck {
public static function getDirectory( $path = '.', $level = 0 ){
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output.
$dh = #opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
$spaces = str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "<strong>$spaces $file</strong><br />";
ImageCheck::getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
echo "$spaces $file<br />";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
}
I call this function in my template like this
ImageCheck::getDirectory($dir);
Save a lot of headache and just use PHP's built in recursive search with a regex expression:
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+(.jpe?g|.png)$/i', RecursiveRegexIterator::GET_MATCH);
?>
In case you are not familiar with working with objects, here is how to iterate the response:
<?php
foreach($Regex as $name => $Regex){
echo "$name\n";
}
?>
How to use php keep only specific file and remove others in directory?
example:
1/1.png, 1/2.jpeg, 1/5.png ...
the file number, and file type is random like x.png or x.jpeg, but I have a string 2.jpeg the file need to keep.
any suggestion how to do this??
Thanks for reply, now I coding like below but the unlink function seems not work delete anything.. do I need change some setting? I'm using Mamp
UPDATE
// explode string <img src="u_img_p/5/x.png">
$content_p_img_arr = explode('u_img_p/', $content_p_img);
$content_p_img_arr_1 = explode('"', $content_p_img_arr[1]); // get 5/2.png">
$content_p_img_arr_2 = explode('/', $content_p_img_arr_1[0]); // get 5/2.png
print $content_p_img_arr_2[1]; // get 2.png < the file need to keep
$dir = "u_img_p/".$id;
if ($opendir = opendir($dir)){
print $dir;
while(($file = readdir($opendir))!= FALSE )
if($file!="." && $file!= ".." && $file!= $content_p_img_arr_2[1]){
unlink($file);
print "unlink";
print $file;
}
}
}
I change the code unlink path to folder, then it works!!
unlink("u_img_p/".$id.'/'.$file);
http://php.net/manual/en/function.scandir.php
This will get all files in a directory into an array, then you can run a foreach() on the array and look for patterns / matches on each file.
unlink() can be used to delete the file.
$dir = "/pathto/files/"
$exclude[] = "2.jpeg";
foreach(scandir($dir) as $file) {
if (!in_array($file, $exclude)) {
unlink("$dir/$file");
}
}
Simple and to the point. You can add multiple files to the $exclude array.
$dir = "your_folder_path";
if ($opendir = opendir($dir)){
//read directory
while(($file = readdir($opendir))!= FALSE ){
if($file!="." && $file!= ".." && $file!= "2.jpg"){
unlink($file);
}
}
}
function remove_files( $folder_path , $aexcludefiles )
{
if (is_dir($folder_path))
{
if ($dh = opendir($folder_path))
{
while (($file = readdir($dh)) !== false)
{
if( $file == '.' || $file == '..' )
continue ;
if( in_array( $file , $aexcludefiles ) )
continue ;
$file_path = $folder_path."/".$file ;
if( is_link( $file_path ) )
continue ;
unlink( $file_path ) ;
}
closedir($dh);
}
}
}
$aexcludefiles = array( "2.jpeg" )
remove_files( "1" , $aexcludefiles ) ;
I'm surprised people don't use glob() more. Here is another idea:
$dir = '/absolute/path/to/u_img_p/5/';
$exclude[] = $dir . 'u_img_p/5/2.jpg';
$filesToDelete = array_diff(glob($dir . '*.jpg'), $exclude);
array_map('unlink', $filesToDelete);
First, glob() returns an array of files based on the pattern provided to it. Next, array_diff() finds all the elements in the first array that aren't in the second. Finally, use array_map() with unlink() to delete all but the excluded file(s). Be sure to use absolute paths*.
You could even make it into a helper function. Here's a start:
<?php
/**
* #param string $path
* #param string $pattern
* #param array $exclude
* #return bool
*/
function deleteFiles($path, $pattern, $exclude = [])
{
$basePath = '/absolute/path/to/your/webroot/or/images/or/whatever/';
$path = $basePath . trim($path, '/');
if (is_dir($path)) {
array_map(
'unlink',
array_diff(glob($path . '/' . $pattern, $exclude)
);
return true;
}
return false;
}
unlink() won't work unless the array of paths returned by glob() happen to be relative to where unlink() is called. Since glob() will return only what it matches, it's best to use the absolute path of the directory in which your files to delete/exclude are contained.See the docs and comments on how glob() matches and give it a play to see how it works.
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, using mkdir:
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.
Here is the missing piece. You need to pass 'recursive' flag as third argument (boolean true) in mkdir call like this:
mkdir('path/to/directory', 0755, true);
Here is something a bit more universal since this comes up on Google. While the details are more specific, the title of this question is more universal.
/**
* recursively create a long directory path
*/
function createPath($path) {
if (is_dir($path))
return true;
$prev_path = substr($path, 0, strrpos($path, '/', -2) + 1 );
$return = createPath($prev_path);
return ($return && is_writable($prev_path)) ? mkdir($path) : false;
}
This will take a path, possibly with a long chain of uncreated directories, and keep going up one directory until it gets to an existing directory. Then it will attempt to create the next directory in that directory, and continue till it's created all the directories. It returns true if successful.
It could be improved by providing a stopping level so it just fails if it goes beyond the user folder or something and by including permissions.
Use a helper function like this:
function makeDir($path)
{
$ret = mkdir($path); // use #mkdir if you want to suppress warnings/errors
return $ret === true || is_dir($path);
}
It will return true if the directory was successfully created or already exists, and false if the directory couldn't be created.
A better alternative is this (shouldn't give any warnings):
function makeDir($path)
{
return is_dir($path) || mkdir($path);
}
A faster way to create a folder:
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
Recursively create the directory path:
function makedirs($dirpath, $mode=0777) {
return is_dir($dirpath) || mkdir($dirpath, $mode, true);
}
Inspired by Python's os.makedirs()
The 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 before
false 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/
Within WordPress, there's also the very handy function wp_mkdir_p which will recursively create a directory structure.
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;
}
I needed the same thing for a login site. I needed to create a directory with two variables.
The $directory is the main folder where I wanted to create another sub-folder with the users license number.
include_once("../include/session.php");
$lnum = $session->lnum; // Users license number from sessions
$directory = uploaded_labels; // Name of directory that folder is being created in
if (!file_exists($directory . "/" . $lnum)) {
mkdir($directory . "/" . $lnum, 0777, true);
}
This is the most up-to-date solution without error suppression:
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory');
}
For 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()
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/newfolder';
if (! is_dir($upload_dir)) {
mkdir( $upload_dir, 0700 );
}
Here 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'));
}
}
You can try also:
$dirpath = "path/to/dir";
$mode = "0764";
is_dir($dirpath) || mkdir($dirpath, $mode, true);
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);
}
To create a folder if it doesn't already exist
Considering the question's environment.
WordPress.
Webhosting server.
Assuming it's Linux, not Windows running PHP.
And quoting from: mkdir
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive =
FALSE [, resource $context ]]] )
The manual says that the only required parameter is the $pathname!
So, we can simply code:
<?php
error_reporting(0);
if(!mkdir('wp-content/uploads')){
// Todo
}
?>
Explanation:
We don't have to pass any parameter or check if the folder exists or even pass the mode parameter unless needed; for the following reasons:
The command will create the folder with 0755 permission (the shared hosting folder's default permission) or 0777, the command's default.
mode is ignored on Windows hosting running PHP.
Already the mkdir command has a built-in checker for if the folder exists; so we need to check the return only True|False ; and it’s not an error; it’s a warning only, and Warning is disabled on the hosting servers by default.
As per speed, this is faster if warning disabled.
This is just another way to look into the question and not claiming a better or most optimal solution.
It was tested on PHP 7, production server, and Linux
if (!is_dir('path_directory')) {
#mkdir('path_directory');
}
We 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)
?>
As 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.
We should always modularise our code and I've written the same check it below...
We first check the directory. If the directory is absent, we create the directory.
$boolDirPresents = $this->CheckDir($DirectoryName);
if (!$boolDirPresents) {
$boolCreateDirectory = $this->CreateDirectory($DirectoryName);
if ($boolCreateDirectory) {
echo "Created successfully";
}
}
function CheckDir($DirName) {
if (file_exists($DirName)) {
echo "Dir Exists<br>";
return true;
} else {
echo "Dir Not Absent<br>";
return false;
}
}
function CreateDirectory($DirName) {
if (mkdir($DirName, 0777)) {
return true;
} else {
return false;
}
}
You first need to check if directory exists file_exists('path_to_directory')
Then use mkdir(path_to_directory) to create a directory
mkdir( 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);
}