The Little Guy Posted December 17, 2006 Share Posted December 17, 2006 I have file names stored in a database, and I was wondering if anyone knew how to get the extension type, basically there will be files like:song.mp3file.mp3cool.jpgawesome.jpgsweet.movfree.see.jpgmy.house.gifthe.song.mp3and so on, I need to get the extension anyone know how, even if there is more than 1 decimal? Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/ Share on other sites More sharing options...
BillyBoB Posted December 17, 2006 Share Posted December 17, 2006 hope this helps[code]<?php$query = mysql_query("SELECT * FROM tablename");$array = mysql_fetch_array($query);if (in_array("mp3", $array) ) { echo("this is a mp3");}if (in_array("jpg", $array) ) { echo("this is a jpg");}?>[/code] Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142764 Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 Are they always going to be 3 char extensions? Maybe...[code]SELECT SUBSTR(file,-3) AS extension FROM tbl;[/code] Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142765 Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 [quote author=BillyBoB link=topic=118938.msg486474#msg486474 date=1166332536]hope this helps[code]<?php$query = mysql_query("SELECT * FROM tablename");$array = mysql_fetch_array($query);if (in_array("mp3", $array) ) { echo("this is a mp3");}if (in_array("jpg", $array) ) { echo("this is a jpg");}?>[/code][/quote]Well, that simply wont work if there is a file called jpg.gif Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142766 Share on other sites More sharing options...
BillyBoB Posted December 17, 2006 Share Posted December 17, 2006 yea i know thats why you shouldnt use them both in a file... i mean why? Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142767 Share on other sites More sharing options...
The Little Guy Posted December 17, 2006 Author Share Posted December 17, 2006 I was thinking maybe using strrpos, and get the position of the last . then using substr to remove everything before it. Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142768 Share on other sites More sharing options...
trq Posted December 17, 2006 Share Posted December 17, 2006 Yeah, that'll work. Something like...[code]<?php function getext($filename) { $pos = strpos(strrev($filename),'.'); return substr($filename, -$pos); }?>[/code] Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142771 Share on other sites More sharing options...
The Little Guy Posted December 17, 2006 Author Share Posted December 17, 2006 Here is the function I ended up using:[CODE]function getext($filename) { $pos = strrpos($filename,'.'); $str = substr($filename, $pos); return $str;}[/CODE] Link to comment https://forums.phpfreaks.com/topic/30942-solved-file-extention-from-database/#findComment-142782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.