Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the file extension [duplicate]

Tags:

php

I'm exploding on "." to get file format and name:

list($txt, $ext) = explode(".", $name);

The problem is that some files have names with dots.

How do I explote on the LAST "." so that I get $name=pic.n2 and $ext=jpg from: pic.n2.jpg?

like image 949
lisovaccaro Avatar asked Mar 07 '12 05:03

lisovaccaro


People also ask

Does Windows 10 have a duplicate file finder?

Answer: No, Windows 10 does not have a duplicate finder in it yet.

How do I get the filename extension?

If filename is empty or null, getExtension(String filename) will return the instance it was given. Otherwise, it returns extension of the filename. To do this it uses the method indexOfExtension(String) which, in turn, uses lastIndexof(char) to find the last occurrence of the '. '.


1 Answers

Use pathinfo:

$pi = pathinfo($name);
$txt = $pi['filename'];
$ext = $pi['extension'];
like image 190
DCoder Avatar answered Oct 16 '22 01:10

DCoder