Hi; A while back you fine folks helped me understand how to create an array of (image) filenames by filtering on the EXTENSIONS.

Now I want to filter based on the beginning of the filenames (say I've got a bunch of .jpgs in a directory with names like "unknown01.jpg, unknown02.jpg, unknown01.jpg, etc. etc." along with other files with various other names.

Is there a simple mod to this code that will just pull the ones that start with "unknown" ?

function directory($dir,$filters) 
{ 
    $handle=opendir($dir); 
    $files=array(); 
    if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
    if ($filters != "all") 
    { 
        $filters=explode(",",$filters); 
        while (($file = readdir($handle))!==false) 
        { 
            for ($f=0;$f<sizeof($filters);$f++): 
                $system=explode(".",$file); 
                if ($system[1] == $filters[$f]){$files[] = $file;} 
            endfor; 
        } 
    } 
    closedir($handle); 
    return $files; 
} 
$filenames = directory (".","jpg,gif,png,bmp,JPG,GIF,PNG,BMP"); //call the function

Thanks.

    ...just thought of a quick workaround for the problem, but am still wondering how one does a filename filter.

    Maybe this workaround will be useful to someone who needs a quick suggestion:

    ...this is part of a form that allows a user to select a picture.
    
    // okay, I have the filename array via the code above
    foreach ($filenames as $value) 
    // EACH PICTURE IS IT'S OWN FLOATING TABLE SO THEY RE-ARRANGE NICELY
    {
    if (substr($value,0,7)=="unknown") {
    // WORKAROUND: the substring acts as a method to keep files NOT named "unknown..." off the page.
    echo "<table style=\"float: left;\"><tr><td VALIGN=CENTER ALIGN=RIGHT nowrap width=45><input type=\"radio\" name=\"MYPIC\" value=\"$value\" CLASS=\"bigButton\"></td><td width=100 align=center><img border=\"0\" src=$value height=125></td></tr></table>";
    } else {}
    } 
    // continue on to the end of the form code...
    
    

      A list of files in the current directory that start with "unknown"?

      $unknown_files = glob("unknown*");
      

      A list of files in an arbitrary directory? Either chdir to it and use the above, or

      $unknown_files = preg_grep('/^unknown/', scandir($directory));
        Write a Reply...