使用pathinfo搜索扩展名时,允许在文件名中使用句点


Allow dots in filenames while searching for extension using pathinfo

我想使用pathinfo()从文件名中提取扩展名。

现在打印:

array(4) {
  ["dirname"]=>
  string(33) "E:/folder/some-folder-with.dots"
  ["basename"]=>
  string(13) "some-folder-with.dots"
  ["extension"]=>
  string(10) ".dots"
  ["filename"]=>
  string(2) "some-folder-with"
}

我现在使用的代码:

function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags); 
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
    }
    return $files;
}
$dir = 'E:/folder/*/*.*';
$files = rglob($dir);
# LOOP
foreach($files AS $file) {
    # KONTROLL
    if($file != '.' AND $file != '..') {
        # VARIABEL
        $testing = pathinfo($file);
        $fname = glob($dir.'/*/*.'.$testing['extension']);
        echo '<pre>';
        var_dump($testing);
        echo '</pre>';
    }
}

这个代码的功能是,我想获取一个文件夹中的每个文件,然后获取它找到的每个文件的扩展名,以便为我的网站找到正确的扩展名。现在,我的文件名中不能有我想要的点。

我怎样才能做到这一点?

您想要SPL中的DirectoryTerator类。

http://php.net/manual/en/class.directoryiterator.php

这只是一个你可以用它做的例子,但你可以定制很多你从中得到的东西:

array ( 0 => '/var/www/html/UusProjekt/intl/homepage_template.php', 1 => '/var/www/html/UusProjekt/intl/config.php', 2 => '/var/www/html/UusProjekt/intl/english.php', 3 => '/var/www/html/UusProjekt/intl/spanish.php', 4 => '/var/www/html/UusProjekt/intl/index.php', )