Created
October 23, 2013 20:46
-
-
Save gmilby/7126429 to your computer and use it in GitHub Desktop.
recursivescan.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$directory = "/home/gmilby/dev-15"; | |
class recursive_scan{ | |
private $directory; | |
private $current_location = array(); | |
/** | |
* Set Directory to gather the files | |
* @param string $directory | |
*/ | |
function __construct($directory){ | |
//add the directory path seperator in case it was not found | |
$this->directory = $directory.(strripos($directory, "/") == strlen($directory) - 1?"":"/"); | |
if(!file_exists($this->directory)) | |
echo "DIRECTORY DOES NOT EXITS: ".$this->directory; | |
} | |
/** | |
* Set to true to display string paths of files | |
* @param boolean $toString optional | |
*/ | |
function getContent($toString = false){ | |
$array = array(); | |
$dir = empty($this->current_location)?"":end($this->current_location); | |
if (!empty($this->current_location)){ | |
$dir = ""; | |
foreach ($this->current_location as $loc) | |
$dir .= "$loc/"; | |
} | |
if ($toString) { | |
echo "Current Directory: $dir<br>"; | |
} | |
$path = $this->directory.$dir; | |
$content = scandir($path); | |
foreach ($content as $k=>$file){ | |
if ($file != ".." && $file != "."): | |
if (is_dir($path.$file)){ | |
//push the directory in the array to be tested | |
array_push($this->current_location, $file); | |
//recursive | |
//empty($dir)?"root":end($this->current_location) | |
$array[$file] = $this->getContent($toString); | |
//delete the folder from array | |
array_pop($this->current_location); | |
}else{ | |
$array[] = $path.$file; | |
if ($toString) { | |
echo "$path$file<br>"; | |
} | |
} | |
endif; | |
} | |
return $array; | |
} | |
} | |
$a = new recursive_scan($directory); | |
$array = $a->getContent(); | |
echo "<pre>".print_r($array,true); | |
print_r($array,true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment