Use the function read_my_dir to scan a folder and subfolders for files and to return array of file and folder traversed. Function is a working copy of the code tested fully on Fedora5 and Window XP plateform.

function read_my_dir($dir)
{
global $tfile,$tdir;$i=0;$j=0;$myfiles;
$myfiles[][] = array();
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file=readdir($dh)) !== false)
{
if (!is_dir($dir.”\\”.$file))
{
$tfile[$i]=$file;
$i++;
echo $dir.”\\”.$file.” – <b>File</b><br>”;
}
else
{
if (($file != “.”) && ($file != “..”))
{
$tdir[$j]=$file;
echo $dir.”\\”.$file.” – <b>Directory</b><br>”;
//$sep = “\\”;
read_my_dir($dir.”\\”.$file);
$j++;
}
}
}
closedir($dh);
}
}
}

// USAGE
read_my_dir(“c:\php”);

Written by Bala Krishna

Bala Krishna is web developer and occasional blogger from Bhopal, MP, India. He like to share idea, issue he face while working with the code.

This article has 3 comments

  1. shiv

    Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING in C:\xampp\htdocs\phpExamples\PHP\scan.php on line 12

    when i given read_my_dir(“c:\xampp”), it showing above error, plz any suggestion

    • Bala Krishna

      Try read_my_dir(“c:\\xampp”) or read_my_dir(“c:/xampp”) in place of read_my_dir(“c:\xampp”)