【问题标题】:Allow dots in filenames while searching for extension using pathinfo使用 pathinfo 搜索扩展名时允许文件名中的点
【发布时间】:2015-09-06 23:24:34
【问题描述】:

我想使用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>';

    }

}

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

我怎样才能做到这一点?

【问题讨论】:

    标签: php filenames glob


    【解决方案1】:

    你想要 SPL 中的 DirectoryIterator 类。

    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', )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 2017-11-13
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多