【问题标题】:Determining the name of an unknown file in a folder确定文件夹中未知文件的名称
【发布时间】:2023-12-22 02:52:01
【问题描述】:

我有很多文件夹,每个文件夹都包含两个文件:一个名为 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 等于另一个文件...

【问题讨论】:

  • PHP 脚本有什么问题?它不工作吗?如果是,会发生什么?
  • 在 php 中做你在第一行所做的事情是否合法?我的意思是一个没有任何引号的字符串
  • $path 来自我刚刚简化的数据库

标签: php arrays glob


【解决方案1】:
$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

【讨论】:

    【解决方案2】:

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

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

    看看有没有帮助:)

    【讨论】: