Last active
February 14, 2017 13:55
-
-
Save jonathangreco/325a163d7c66405a67c6cfe26b2a1d80 to your computer and use it in GitHub Desktop.
fileRecursion scan
This file contains 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
public function listDir( | |
$dirpathFromRoot, | |
$sortColumn = 'filename', | |
$orderBy = SORT_ASC, | |
$listDir = false, | |
$recursive = false | |
) { | |
if ($orderBy != SORT_ASC && $orderBy != SORT_DESC) { | |
$orderBy = SORT_ASC; | |
} | |
$res = $this->recursiveScandir($dirpathFromRoot, $recursive); | |
$files = array(); | |
foreach ($res as $file) { | |
if ((!$listDir && !is_dir($file)) || $listDir) { | |
$infos = pathinfo($file); | |
$infos_plus = stat($file); | |
if (is_dir($file)) { | |
$infos['extension'] = ''; | |
} | |
$files[] = array( | |
'filename' => $infos['filename'], | |
'extension' => $infos['extension'], | |
'path' => $file, | |
'size' => $infos_plus['size'], | |
'ctime' => $infos_plus['ctime'], | |
'mtime' => $infos_plus['mtime'] | |
); | |
} | |
} | |
foreach ($files as $index => $row) { | |
foreach ($row as $key => $value) { | |
${$key}[$index] = $value; | |
} | |
} | |
if (count($files)>1) { | |
if (isset(${$sortColumn}) && is_int(${$sortColumn}[0])) { | |
array_multisort(${$sortColumn}, $orderBy, SORT_NUMERIC, $filename, SORT_ASC, SORT_STRING, $files); | |
} elseif (isset($$sortColumn) && $sortColumn != 'filename') { | |
array_multisort(${$sortColumn}, $orderBy, SORT_STRING, $filename, SORT_ASC, SORT_STRING, $files); | |
} else { | |
array_multisort($filename, SORT_ASC, SORT_STRING, $files); | |
} | |
} | |
return $files; | |
} | |
/** | |
* Scandir récursif pour lister les fichiers et dossiers des sous-dossier | |
* | |
* @param string $dirpathFromRoot | |
* @param boolean $recursive | |
* @param array $data | |
* @return array | |
*/ | |
private function recursiveScandir($dirpathFromRoot, $recursive = false, &$data = array()) | |
{ | |
$dirpathFromRoot = $this->fixpath($dirpathFromRoot); | |
$array = scandir($dirpathFromRoot); | |
foreach ($array as $value) { | |
if (!preg_match('#^[\._:]#', $value)) { | |
if (is_dir($dirpathFromRoot.$value)) { | |
$data[] = $dirpathFromRoot.$value.'/'; | |
if ($recursive) { | |
$data = $this->rscandir($dirpathFromRoot.$value.'/', $recursive, $data); | |
} | |
} elseif (is_file($dirpathFromRoot.$value)) { | |
$data[] = $dirpathFromRoot.$value; | |
} | |
} | |
} | |
return $data; | |
} | |
public function fixpath($path) | |
{ | |
$path = str_replace('\\', '/', $path); | |
$path = preg_replace('#/+#', '/', $path); | |
if (file_exists($path) && !is_file($path)) { | |
if (substr($path, 0, -1) != '/') { | |
$path .= '/'; | |
} | |
} | |
return ($path); | |
} |
This file contains 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
protected $canonicalNamesReplacements = ['-' => '', '_' => '', ' ' => '', '\\' => '', '/' => '']; | |
strtolower(strtr($name, $this->canonicalNamesReplacements)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment