note 49819 added to function.scandir

From: Date: Wed, 09 Feb 2005 13:58:11 +0000
Subject: note 49819 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-84715@lists.php.net to get a copy of this message
Since I needed to scan dirs and subdirs and wanted to have files and dirs not in the same var without having to explode it, I did this little function: <?php function scan_Dir($directory=NULL) { $temp = ""; if ($directory==NULL) { $directory = getcwd(); } $handle = opendir($directory); while (false !== ($filename = readdir($handle))) { if (($filename!=".") AND ($filename!="..")) { if (is_dir($directory."/".$filename)) { $old_directory = $directory; $directory .= "/".$filename; $temp = scan_Dir($directory); $cpt = 0; while ($cpt<count($temp["filename"])) { $arrfiles["path"][] = $directory."/".$temp["filename"][$cpt]; $arrfiles["directory"][] = $directory; $arrfiles["filename"][] = $temp["filename"][$cpt]; $cpt++; } $directory = $old_directory; } else { $arrfiles["path"][] = $directory."/".$filename; $arrfiles["directory"][] = $directory; $arrfiles["filename"][] = $filename; } } } return $arrfiles; } ?> This one seems to work properly and use multidimensional array. To show your files, just use: <?php $dir2scan = "admin"; $yourvar = scan_Dir($dir2scan); echo $yourvar["path"][0]."<br>"; //Complete path of the file with key 0 (dir + filename) echo $yourvar["directory"][0]."<br>"; //Directory of the file with key 0 echo $yourvar["filename"][0]."<br>"; //Filename of the file with key 0 ?> Note that the parameter of the function is not necessary. If you don't give any, it will scan from the returned value of getcwd() function. ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49819 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49819&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49819&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49819&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49819&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49819&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49819&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84715) next »