Skip to content

Instantly share code, notes, and snippets.

@nagiyevelchin
Last active February 12, 2022 15:59
Show Gist options
  • Save nagiyevelchin/d6087c1c32ed56d498e596b92fc1943d to your computer and use it in GitHub Desktop.
Save nagiyevelchin/d6087c1c32ed56d498e596b92fc1943d to your computer and use it in GitHub Desktop.
Recursively create directory in PHP
<?php
/**
* Makes directory
* @link http://stackoverflow.com/questions/5425891/check-if-directory-exists-php
* @param string $pathname The directory path.
* @param int $mode [optional]<p>
* The mode is 0777 by default, which means the widest possible
* access. For more information on modes, read the details
* on the <b>chmod</b> page.
* </p>
* @since 11/23/12 12:43 PM
* @return bool <p><b>INT</b> on success or <b>FALSE</b> on failure.
* 1 - The directory $pathname was successfully created.
* </p>
*/
function mkdir($pathname, $mode = 0777) {
$p = "";
$pathname = explode('/', $pathname);
foreach ($pathname as $path) {
$p .= $path . "/";
if (!file_exists($p)) {
if (!mkdir($p, $mode)) {
return false;
}
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment