确定文件夹中的未知文件的名称

时间:2011-06-02 17:10:54

标签: php arrays glob

我有很多文件夹,每个文件夹都包含两个文件:一个名为thumb,但扩展名未知,另一个我不知道。我的问题是如何才能获得未知图像的路径?这是脚本:

$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 = ???;

我希望$ thefile等于另一个文件......

2 个答案:

答案 0 :(得分:2)

$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

答案 1 :(得分:0)

在尝试在字符串中输出变量值时,请务必使用双引号:

$all = glob("$pathtofiles*.*");
$zip = glob("$thumbz*.*");

看看是否有帮助:)

相关问题