简体   繁体   中英

Allow dots in filenames while searching for extension using pathinfo

I want to use pathinfo() to extract the extension from filenames.

As it prints now:

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"
}

The code I use now:

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>';

    }

}

The function of this code, is that I want to get every file that are in one folder and then get the extension of every file it finds in order to find the right extension for my website. As it is right now, I can't have dots in my filenames which I want to have.

How can I accomplish this?

You want DirectoryIterator class from SPL.

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

This is just a sample you can do with it, but you can customize a lot what you get out from it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM