How to get the extension of a filename...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

How to get the extension of a filename...

Post by revbackup »

hi guys,


I have a problem with getting the extension of a filename...

at first what i did is to do like this

Code: Select all

 
$file = hello.php; 
$ext= explode('.',$file);
//so i get $ext[1] which is .php
 
but i have a problem when the filename has a name like this:

$file = hello.cfg.inc.php;

with my code, i can get cfg instead of the php that i want to get....

So, how can i get the real file extension of a file?
thanks...
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: How to get the extension of a filename...

Post by dude81 »

revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

Re: How to get the extension of a filename...

Post by revbackup »

I cannot use $_FILE because my file is from a server so I need to use
ftp functions...

and filetype is not available in ftp functions..

so i have to use string functions to get the file extensions of the file...
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How to get the extension of a filename...

Post by Mark Baker »

$extension = pathinfo($filename,PATHINFO_EXTENSION);
Post Reply