DirectoryIterator :: getExtension () version

I am running a PHP script from the CLI that uses the DirectoryIterator::getExtension() method in PHP.

The PHP version I'm running on is 5.3.5, but I keep getting errors saying Fatal error: Call to undefined method DirectoryIterator::getExtension()

The script works fine on my local developer machine with PHP 5.3.6 without errors. However, the page for DirectoryIterator :: getExtension () in human PHP does not mention the version.

Can someone tell me which version I need to use for this method?

+6
source share
3 answers

From the documentation :

(There is no version information, it can only be in SVN)

It seems that the commit did not build PHP 5.3.5, but got into release 5.3.6. The page has not yet been updated.

And as Mario noted, 5.3.6 changelog mentions the addition:

  • SPL extension:
    • ...
    • Added SplFileInfo :: getExtension (). FR # 48767. (Peter Cowburn)
+5
source

This is a much simpler way to get the file extension, IMO:

 <?php $iterator = new DirectoryIterator($data_dir); foreach($iterator as $entity) { if($entity->isFile()) { $file_extension = pathinfo($entity->getFilename(), PATHINFO_EXTENSION); ... } } ?> 
+8
source

No wonder I kept getting getExtension (). Although it is documented, it is not in verification 5.3.5. I am using this workaround for the missing getExtension ():

 foreach($iter as $splFileInfo) { $fileName = $splFileInfo->getFilename(); $pos_dot = strrpos($fileName, "."); // find '.' $ext = ($pos_dot !== false) ? substr($fileName, $pos_dot+1) : null; //. . . } 
+1
source

Source: https://habr.com/ru/post/903784/


All Articles