Glob() to get all files in a dir -- filter names -- then get count

please see my comments in this code for my questions… thank you…

if (is_dir($FPathPhotos)) {
    if ($dh = opendir($FPathPhotos)) {
        
        foreach(glob($FPathPhotos."*.jpg") as $filename) {
            $fileName = basename($filename);    
            echo 'file name -- ' . $fileName . '<br>';
        
            if (checkPhotoName($fileName)) {
                echo 'YES -- ' . $fileName . '<br>'; 
            } else { 
                echo 'NO -- ' . $fileName . '<br>'; 
                unset($fileName);    //  is this an ok way to remove bad filenames?
            }
            echo 'fileName AFTER FILTERING --  ' . $fileName . '<br>';   //  works -- prints only files whose names check..    
        //    I need count of files AFTER FLENAME FILTERING... how do I get this?     
        }
    }
}

I tried approach I use in java for this…

namely creating a new array and adding filenames that check into the new array*

so created a new array and used array_push() to add filenames that check…

 $filesAfterFiltering = array();

    foreach(glob($FPathPhotos."*.jpg") as $filename) {
    $fileName = basename($filename);    

    if (checkPhotoName($fileName)) {
        echo 'YES -- ' . $fileName . '<br>'; 
        array_push($filesAfterFiltering,$fileName);  
   } else {
        //   ............
  }

this did not work… used a standard loop to print filenames in new $filesAfterFiltering array, but nothing would print… so I opted for removing bad filenames instead… but now find I can’t get count of files after filtering filenames… yikes…:wink:

thank you…


*actually in java you can’t dynamically add elements to an array, have to use Vector and then convert to array… beside the point, but just to clarify…:wink:

I would initialize an array outside of the loop and then add whatever met the crieria to it.
then I would use count() on the array

hi Mittineague…:slight_smile:

please see my 2nd post (reply to my 1st post…:wink:

that’s the approach I first tried, it didn’t work…

oh my gosh… it works now… I feel like a fool… I don’t know why I couldn’t get it to work before… so does this code look ok? :wink:

  if (is_dir($FPathPhotos)) {
        if ($dh = opendir($FPathPhotos)) {
            
            $filesAfterFiltering = array();
            
            foreach(glob($FPathPhotos."*.jpg") as $filename) {
                $fileName = basename($filename);    
                echo 'file name before filtering -- ' . $fileName . '<br>';
            
                if (checkPhotoName($fileName)) {
                    echo 'YES -- ' . $fileName . '<br>'; 
                    array_push($filesAfterFiltering,$fileName);
                } else { 
                    echo 'NO -- ' . $fileName . '<br>';
                }
            }
        }
    }

   foreach($filesAfterFiltering as $element) {
         echo 'filename after filtering -- ' . $element . '<br>';
   }
   echo 'how may files check: ' . count($filesAfterFiltering) . '<br>';

Depending on what you want it to do it looks fine to me.

As for things not working and then working (or worse, the opposite) I’ve learned to accept that somethings defy any logical explanation.

thanks man…

I appreciate all your help…:slight_smile: