Codice PHP:
$_CONFIG['path_name'] = "***********";
/**
* Recursive function to scan a directory with * scandir() *
*
* @param String $rootDir
* @return multi dimensional array
*/
function scanDirectories($rootDir) {
// set filenames invisible if you want
$invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
// run through content of root directory
$dirContent = scandir($rootDir);
$allData = array();
// file counter gets incremented for a better
$fileCounter = 0;
foreach($dirContent as $key => $content) {
// filter all files not accessible
$path = $rootDir.'/'.$content;
if(!in_array($content, $invisibleFileNames)) {
// if content is file & readable, add to array
# if(is_file($path) && is_readable($path)) {
# $tmpPathArray = explode("/",$path);
# // saving filename
# $allData[$fileCounter]['fileName'] = end($tmpPathArray);
# // saving while path (for better access)
# $allData[$fileCounter]['filePath'] = $path;
# // get file extension
# $filePartsTmp = explode(".", end($tmpPathArray));
# $allData[$fileCounter]['fileExt'] = end($filePartsTmp);
# // get file date
# $allData[$fileCounter]['fileDate'] = filectime($path);
# // get filesize in byte
# $allData[$fileCounter]['fileSize'] = filesize($path);
# $fileCounter++;
# // if content is a directory and readable, add path and name
# }elseif(is_dir($path) && is_readable($path)) {
##la linea sotto va sostituita con quella prima se si usa lo script completo
if(is_dir($path) && is_readable($path)) {
$dirNameArray = explode('/',$path);
$allData[$path]['dirPath'] = $path;
$allData[$path]['dirName'] = end($dirNameArray);
// recursive callback to open new directory
$allData[$path]['content'] = scanDirectories($path);
}
}
}
return $allData;
}
$a = scanDirectories($_CONFIG['path_name']);
foreach( $a as $k=>$v)
{
foreach( $v as $l=>$m)
{
if($l == 'dirName')
{echo "<h1>$l -> $m</h1>\n";}
if($l == 'dirPath')
{echo "
[b]$l -> $m"."/"."[/b]</p>\n";}
if($l == 'content')
{echo "
$l -> $m</p>\n";}
}
echo "<hr>";
echo "
[b]$v[dirPath][/b]</p>";
$directory = $v['dirPath']."/"; //Slash finale, altrimenti non va
$arr = glob($directory."*", GLOB_ONLYDIR);
if( !empty($arr) )
{
foreach($arr as $filename)
{echo "
".basename($filename)."</p>";}
}
else
{echo "
Nessuna directory</p>";}
echo "<hr>";
echo "<hr>";
}
Ecco, ho messo insieme i due script.
Le parti della funzione scanDirectories commentati sono quelle parti che nella presente domanda non mi servivano, ma ovviamente risultano utili allorquando si volesse vedere tutto il contenuto della directory.
Grazie degli aiuti, funziona benissimo!
Solo una cosa
Se lo lascio così, comunque lui stampa prima dirPath, poi dirName e infine content.
Se io modifico l'ordine dello script, dovrebbe di conseguenza modificare l'ordine dell'output, vero? E c'è un modo per modificare l'ordine di uscita dell'output senza toccare lo script?