GremlinP1R Posted October 19, 2006 Share Posted October 19, 2006 Hi there allI'm treing to read a file extention but don't know how.... it reads the file name and all but just cant get it to read the .jpg extension.Any idees? Thanx Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/ Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 You can do it like this:[code]$var = explode('.',$filename);$extension = $var[count($var)-1];echo $extension;[/code] Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111170 Share on other sites More sharing options...
GremlinP1R Posted October 19, 2006 Author Share Posted October 19, 2006 Thanx it works Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111187 Share on other sites More sharing options...
obsidian Posted October 19, 2006 Share Posted October 19, 2006 or, an alternate method:[code]<?php$ext = substr($filename, strrpos('.', $filename));?>[/code] Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111213 Share on other sites More sharing options...
Daniel0 Posted October 19, 2006 Share Posted October 19, 2006 [quote author=obsidian link=topic=112006.msg454322#msg454322 date=1161262831]or, an alternate method:[code]<?php$ext = substr($filename, strrpos('.', $filename));?>[/code][/quote]Doesn't work.[code]<?php$filename = "mysql.db.php";$ext = substr($filename, strrpos('.', $filename));echo $ext;?>[/code]outputs [quote=code output]mysql.db.php[/quote] Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111217 Share on other sites More sharing options...
obsidian Posted October 19, 2006 Share Posted October 19, 2006 [quote author=Daniel0 link=topic=112006.msg454326#msg454326 date=1161263193]Doesn't work.[/quote]my bad, i switched the arguments for strrpos:[code]<?php$filename = "mysql.db.php";$ext = substr($filename, strrpos($filename, '.')+1);echo $ext;?>[/code]you could also just use a preg_match:[code]<?php$filename = "mysql.db.php";preg_match('|\.([a-z]+)$|i', $filename, $match);$ext = $match[1];?>[/code] Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111225 Share on other sites More sharing options...
printf Posted October 19, 2006 Share Posted October 19, 2006 Just another way...[code]$ext = str_replace ( '.', '', strrchr ( $file, '.' ) );[/code]me! Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.