0

I am trying to get all files in a folder. The files are images along with one extra file named as "Thumbs.db"

fOLLowing is my code

$path = $ringPaths[$i];
        $dirname =   substr( $path ,0, -4 );
        $lastpart = basename( $dirname , ".zip");
        $abspath = substr( $dirname ,strpos( $dirname, "com/")+4 );
        $direcotypath = "../".$abspath."/".$lastpart;

        $files = glob($direcotypath."/*.*");
        foreach ($files as $key => &$value) 
        {
                $value = str_replace( "..", $url, $value);
            }

            $final = array_merge($final,$files);

I have tried removing Thumbs.db using following code

foreach ($final as $key => &$value) 
    {     

              if($value == "Thumbs.db")
              {
                unset($final[$key]); 
              }
        }

Anyone know what is wrong with above code? I am still in learning process

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • "*It is not working*" is not useful. Please explain, if you get an error message (if so, which one) or if the results differ from your expectation (if so, how). Make sure you've [enabled error reporting](http://stackoverflow.com/a/6575502/1438393). – Amal Murali May 05 '14 at 05:41
  • 1
    I think `$value` will be `$direcotypath . "/Thumbs.db"` instead of just `"Thumbs.db"` – Halcyon May 05 '14 at 05:43
  • You should NOT use `&` references as part of your foreach() loop. It can lead to very weird and hard-to-track bugs – Marc B May 05 '14 at 05:51
  • fyi, for parsing filenames and paths then i suggest using either [pathinfo](http://www.php.net/manual/en/function.pathinfo.php) or [SplFileInfo](http://www.php.net/manual/en/class.splfileinfo.php) as they have methods for returning the various parts of the filepath and filename. – Ryan Vincent May 05 '14 at 06:14

1 Answers1

1

Reading the documentation for glob more closely, you will see that you can set flags to filter the type of files that will be returned.

You can try something like:

$files = glob($direcotypath . "/*.{jpg,jpeg,gif,png,bmp}", GLOB_BRACE);
//                                 ^^^ Add more allowed extensions here.
Marty
  • 39,033
  • 19
  • 93
  • 162