Jump to content
calvin182

getting file's extention

Recommended Posts

get the URL or the address then take the last 4 charactorslike this:$file = "/folder/file.txt";$extension = substr($file, strlen($file) - 4);$extension will be .txt(with jpeg you will not have the dot)

Share this post


Link to post
Share on other sites

that wont work though. ther are five letter file extensions such as .class which is a java applet file. maybe some way to get all the text after the last dot?

Share this post


Link to post
Share on other sites

the only extentions i'll be needing this for are .bmp .gif .jpeg .jpg .png basically image types, so does anybody know how to get the stuff from the . and on?

Share this post


Link to post
Share on other sites

FINE, were take the easy way out... I was trying to have fun!

<?php$path_parts = pathinfo('/www/htdocs/index.html');print $path_parts['extension'];?>

if you need more than the ending like: path?arg=value#anchorthen I would go with parse_url ( string url )this will output:Array( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor)but if you need from the dot on, you will have to break it at the . (dot) and take the $var[1]

Share this post


Link to post
Share on other sites

I know your problem is already solved, but this may be an easier way:

$file_name="12345.abcd";preg_match('/\.[^.]+$/', $file_name, $ext);

So the extension there would be ".abcd" and the Preg_match would send it out as an array, so use $ext[0].

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...