note 49819 deleted from function.scandir by cmb
| From: | cmb@php.net | Date: | Sun, 17 Nov 2019 09:03:12 +0000 |
| Subject: | note 49819 deleted from function.scandir by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213111@lists.php.net to get a copy of this message | ||
Note Submitter: virtual at NOSPAM dot lapinbleu dot ch
----
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.