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 Quote 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] Quote 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 Quote 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] Quote 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] 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] Quote 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! Quote Link to comment https://forums.phpfreaks.com/topic/24432-reading-an-extension/#findComment-111250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.