Skip to content

Instantly share code, notes, and snippets.

@openam
Created February 26, 2014 17:19
Show Gist options
  • Select an option

  • Save openam/9234101 to your computer and use it in GitHub Desktop.

Select an option

Save openam/9234101 to your computer and use it in GitHub Desktop.
Make Directories Recursively in PHP 4
<?php
/**
* rmkdir is a recursive mkdir function. PHP4 doesn't have the recursive paramater,
* so this was added to use on a PHP 4 server. These references could be changed back to
*
* @param string $path
* @param int $mode of the file permissions
* @param boolean $recursive not used, but is there so that you can easly change from rmkdir (PHP 4) to mkdir(PHP 5) easily
* @return boolean true if directories were successfully created
*/
function rmkdir($path, $mode = 0777, $recursive = true) {
$path = str_replace("\\", "/", $path);
$path = explode("/", $path);
$return = false;
$rebuild = '';
foreach($path as $key => $p) {
if (strlen($p) > 0) {
if(strstr($p, ":") != false) {
echo "\nFound : in $p\n";
$rebuild = $p;
continue;
}
if (in_array($p, array('.', '..')) && $key == '0') {
$rebuild .= "$p";
} else {
$rebuild .= "/$p";
}
if(!is_dir($rebuild)) {
if (mkdir($rebuild, $mode)) {
$return = true;
} else {
$return = false;
}
}
}
}
return $return;
}
@MathiasReker
Copy link

Nice script. Thanks for sharing. :-)
I would like to share a literary I have coded with the same goal.

Example of usage:

<?php

use MathiasReker\FilePerm;

require __DIR__ . '/vendor/autoload.php';

(new FilePerm([__DIR__]))
    ->setDefaultModeFile(0644)
    ->setDefaultModeFolder(0755)
    ->scan()
    ->fix();

Full documentation: https://github.com/MathiasReker/php-file-permissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment