<?php /* PARAMETRES: $folder : Chemin du dossier à parcourir $ext : Tableau contenant la liste des extensions à rechercher. Si vide ou null, liste TOUS les fichiers $recursif : Paramtre indiquant si l'on souhaite rechercher dans les sous dossiers Par défaut : TRUE RETOUR : Un tableau contenant l'ensemble des fichiers trouvés */ function ListFiles($folder,$ext=array(''),$recursif=true) { $files = array(); $dir=opendir($folder); while ($file = readdir($dir)) { if ($file '.' || $file '..') continue; if (is_dir($folder.'/'.$file)) { if ($recursif==true) $files=array_merge($files, ListFiles($folder.'/'.$file, $ext)); } else { if(empty($ext)){ //Si aucune extension définie, on prend tout.. $files[] = $folder.'/'.$file; }else{ foreach ($ext as $v) { if (strtolower($v)==strtolower(substr($file,-strlen($v)))) { $files[] = $folder.'/'.$file; break; } } } } } closedir($dir); return $files; } ?>
// $dossier -> Chemin ABSOLU du repertoire à parcourir $dossier='C:\\wamp\\www\\PERSO\\myteams'; $files=ListFiles($dossier,null,true); foreach ($files as $file){ print($file.'<br>'); print(realpath($file)."<br>"); print(find_relative_path(getcwd(),$file).'<br>'); echo '<br>'; } //---------------- FONCTIONS ----------------------// function ListFiles($folder,$ext=array(''),$recursif=true) { $files = array(); $dir=opendir($folder); while ($file = readdir($dir)) { if ($file '.' || $file '..') continue; if (is_dir($folder.'/'.$file)) { if ($recursif==true) $files=array_merge($files, ListFiles($folder.'/'.$file, $ext)); } else { if(empty($ext)){ //Si aucune extension définie, on prend tout.. $files[] = $folder.'/'.$file; }else{ foreach ($ext as $v) { if (strtolower($v)==strtolower(substr($file,-strlen($v)))) { $files[] = $folder.'/'.$file; break; } } } } } closedir($dir); return $files; } /** * * Find the relative file system path between two file system paths * * @param string $frompath Path to start from * @param string $topath Path we want to end up in * * @return string Path leading from $frompath to $topath */ function find_relative_path ( $frompath, $topath ) { $from = explode( DIRECTORY_SEPARATOR, $frompath ); // Folders/File $to = explode( DIRECTORY_SEPARATOR, $topath ); // Folders/File $relpath = ''; $i = 0; // Find how far the path is the same while ( isset($from[$i]) && isset($to[$i]) ) { if ( $from[$i] != $to[$i] ) break; $i++; } $j = count( $from ) - 1; // Add '..' until the path is the same while ( $i <= $j ) { if ( !empty($from[$j]) ) $relpath .= '..'.DIRECTORY_SEPARATOR; $j--; } // Go to folder from where it starts differing while ( isset($to[$i]) ) { if ( !empty($to[$i]) ) $relpath .= $to[$i].DIRECTORY_SEPARATOR; $i++; } // Strip last separator return substr($relpath, 0, -1); } ?>
list_dir($fichier);
if(is_dir($dossier.'/'.$fichier)){ list_dir($dossier.'/'.$fichier) };
<?php function parcourir_repertoire($repertoire) { $le_repertoire = opendir($repertoire) or die("Erreur le repertoire $repertoire existe pas"); while($fichier = @readdir($le_repertoire)) { // enlever les traitements inutile if ($fichier "." || $fichier "..") continue; if(is_dir($repertoire.'/'.$fichier)) { print '<ul>'.$repertoire.'/'.$fichier; parcourir_repertoire($repertoire.'/'.$fichier); print '</ul>'; } else { print "<li> $fichier </li>"; } } closedir($le_repertoire); } parcourir_repertoire('C:\util\wamp\www\exemple\www'); ?>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question