Allow dots in filenames while searching for extension using pathinfo

281 views Asked by At

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?

1

There are 1 answers

0
Siim Kallari On BEST ANSWER

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