troubleshooting Question

PHP: Best way to scan folders and process files

Avatar of trevor1940
trevor1940 asked on
PHP
10 Comments3 Solutions359 ViewsLast Modified:
Searching the internet there seems to be many ways of traversing directories and files

Can some one suggest what the best way to this?

Within the "root" directory there should only be sub directories the name of each is required and processed separately
within each sub directory should only be files each one needs processing according to file extension

This is a windows system so how do I deal with'.' and '..'?

This is how far I've got and isn't tested more to give you an idea of what I'm trying to do

<?PHP
error_reporting(E_ALL);

$DS = DIRECTORY_SEPARATOR;

$root ="path" . $DS . "to" . $DS . "root";

$Dirs= scandir($root);
foreach($Dirs as $dir){

//$dir name is wanted 
$dh  = opendir($dir);
while (false !== ($fileName = readdir($dh))) {
    $ext = substr($fileName, strrpos($fileName, '.') + 1);
    if($ext == "html"){
       $htmlFile = $dir . $DS . $fileName;
       // Open and Do stuff with html file
    }
    elseif(in_array($ext, array("jpg","jpeg","png"))){
        //Do other stuff
    }
    else{
     // Error code file type not needed
    }
 }
}
SOLUTION
gr8gonzo
Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros