Search code examples
phparraysglob

Determining the name of an unknown file in a folder


I have lots of folders and each of them contains two files: one is called thumb but the extension is unknown and the other one I don't know. My question is how could I get the path of the unknown image? Here is the script:

$path = 'images/2011/May/30/brfZ0ehnBKO/thumb.jpg';
$pathtofile = substr($path, 0, -9); //images/2011/May/30/brfZ0ehnBKO/
$thumbz = $pathtofiles."thumb";
$all = glob('$pathtofiles*.*');
$zip = glob('$thumbz*.*');
$remaining = array_diff($all, $zip);

$thefile = ???;

I want $thefile to be equal to the other file...


Solution

  • $pathtofile = dirname('images/2011/May/30/brfZ0ehnBKO/thumb.jpg'); 
    
    if ($handle = opendir($pathtofile)) {
        while (false !== ($file = readdir($handle))) {
            if(strpos($file, 'thumb') !== 0) {
                $thefile = $file;
                break;
            }
        }
        closedir($handle);
    }
    
    var_dump($thefile); // null if no such file