Avatar of trevor1940
trevor1940
 asked on

PHP: Best way to scan folders and process files

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
    }
 }
}

Open in new window

PHP

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

@gr8gonzo - I did the same thing a couple months back but nice to see the results confirmed.
gr8gonzo

Yep, never had run the test myself before. :)
Ray Paseur

I expect you will find it to be even faster on PHP7, however to get a consistent test, you'll need to use the same I/O subsystem.  I didn't bother to time it because the response was essentially instantaneous on my server where there are less than 100,000 files.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
trevor1940

ASKER
Hi All thanx for the difference test, interesting

  @gr8gonzo was that 1 directory containing 216265 files or a tree would it matter?

@Ray
each array element contains the complete file path.
Once I have a list of full path  files I assume there are methods for breaking it into components

Eg parent folder, Filename & extension  / file type?

Using "$ext = substr($fullPath, strrpos($fullPath, '.') + 1);"   only works if the file has an extension

Path.To/ReadMe  fails
Path.To/ReadMe.txt works
Ray Paseur

a list of full path  files I assume there are methods for breaking it into components
Yes, but this seems to be a different question.
"$ext = substr($fullPath, strrpos($fullPath, '.') + 1);"   only works if the file has an extension
Yes, that makes sense.  You might want to choose the second example I posted above, using the function dirToList().  It gives full file paths all the way to the file, and does not give paths to directories.  You may want to sort the list.  And if your files do not have extensions, that is a separate issue and worthy of another question.  We will need to see your test data!
trevor1940

ASKER
Yes, but this seems to be a different question.

OK Ray quite possibly I was just thinking beyond the current project so don't have any data
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
trevor1940

ASKER
thanx for your help