Determine File Extensions Using JavaScript

A note from A Beautiful Site’s founder: We developed Surreal to be easy for you and your clients. If you’re a web designer, you should take a look at the simple, hosted CMS that’s changing the way content is managed. Surreal integrates in moments and is trusted by over 18,000 websites. Try it out for free and let us know what you think! Visit Website »

A simple PHP function to determine a file’s extension.

fileExt()

function fileExt(path) {
    return path.substr(path.lastIndexOf('.') + 1);
}

Sample Output

Input Output()
file.ext ext
/folder/subfolder/file.ext ext
file.bak.ext ext
If you enjoyed this article, please share it with a friend!

2 Responses to Determine File Extensions Using JavaScript

  1. Arif Uddin says:

    genious man… just when i thought reversing the string then find the first occurrence of ‘.’ … i thought search in google… and i get what i need…

    thanks again.. for publishing this little code…

  2. That’s a good script but here’s another one. It takes some people a while to understand that substr function so I wrote another one for the light heads. (I was a light head too so I know how it is)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>