Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the File Extensions using PHP

Status
Not open for further replies.

MrPantsTm

Programmer
Joined
Jan 22, 2004
Messages
71
Location
US
What I would like to do is get the file extension from a file that was just uploaded to put as a field in my database. I've tried the below code but thats dependent on how long the filename is. I would like it to automatically get the extension without knowing how long the file is. Has anyone done this or know of an answer. TIA!

$extension = $basefilename;
$extension = $extension[extension];
 
would this -

Code:
$temp = explode('.', $basefilename);
$extension = $temp[count($temp)-1];

work?
 
It works, thank you.

I also tried:

$temp = explode('.', $basefilename);
echo $temp[1];

but my solution will crumble if theres more than 1 period in the file name. Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top