Jump to content

Archived

This topic is now archived and is closed to further replies.

GremlinP1R

Reading an extension...

Recommended Posts

Hi there all

I'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

Share this post


Link to post
Share on other sites
You can do it like this:
[code]$var = explode('.',$filename);
$extension = $var[count($var)-1];
echo $extension;[/code]

Share this post


Link to post
Share on other sites
or, an alternate method:
[code]
<?php
$ext = substr($filename, strrpos('.', $filename));
?>
[/code]

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
Just another way...

[code]$ext = str_replace ( '.', '', strrchr ( $file, '.' ) );[/code]


me!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.