Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:43:55+00:00 2026-06-13T14:43:55+00:00

Possible Duplicate: How to extract a file extension in PHP? Get the file extension

  • 0

Possible Duplicate:
How to extract a file extension in PHP?
Get the file extension (basename?)

trying tot learn from other people´s code , I see a lot of methods to strip a filename from it´s extension, but most of the methods seems too localized as they assume a certain condition. for example :

This will assume only 3-character extension (like .txt, .jpg, .pdf)

substr($fileName, 0, -4);

or

 substr($fileName, 0, strrpos($fileName, '.')); 

But this can cause problems on file names like .jpeg, .tiff .html . or only 2 like .jsOr .pl

(browsing this list shows some file names can have only 1 character, and some as many as 10 (!) )

some other methods i have seen rely on the point (.)

for example :

  return key(explode(“.”, $filename));

Can cause problems with filenames like 20121029.my.file.name.txt.jpg

same here :

return preg_replace('/\.[^.]*$/', '', $filename);

some people use the pathinfo($file) and / or basename() (is it ALWAYS safe ?? )

basename($filename);

and many many other methods ..

so my question has several parts :

  • what is the best way to “strip” a file extension ? (with the point)

  • what is the best way to “get” the file extension (without the point) and / or check it

  • will php own functions (basename) will recognize ALL extensions regardless of how exotic they might be or how the filename is constructed ?

  • what if any influence does the OS has on the matter ? (win, linux, unix…)

  • all those small sub-questions , which i would like to have an answer to can be summed-up in an overall single question :
    Is there a bullet-proof , overall, always-work, fail-proof , best-practice , über_function that will work under all and any condition ??

EDIT I – another file extension list

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:43:57+00:00Added an answer on June 13, 2026 at 2:43 pm

    Quoting from the duplicate question’s top answer:

    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    

    this is the best available way to go. It’s provided by the operating system, and the best you can do. I know of no cases where it doesn’t work.

    One exception would be a file extension that contains a .. But no sane person would introduce a file extension like that, because it would break everywhere plus it would break the implicit convention.

    for example in a file 20121021.my.file.name.txt.tar.gz – tar.gz would be the extention..

    Nope, it’s much simpler – and maybe that is the root of your worries. The extension of 20121021.my.file.name.txt.tar.gz is .gz. It is a gzipped .gz file for all intents and purposes. Only when you unzip it, it becomes a .tar file. Until then, the .tar in the file name is meaningless and serves only as information for the gunzip tool. There is no file extension named .tar.gz.

    That said, detecting the file extension will not help you determine whether a file is actually of the type it claims. But I’m sure you know that, just putting this here for future readers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to extract a file extension in PHP? How can we get
Possible Duplicate: Get file name from URI string in C# How to extract file
Possible Duplicate: how to extract data from csv file in php i'm new on
Possible Duplicate: How to extract a file extension in PHP? I have a variable
Possible Duplicate: How to extract a file extension in PHP ? For example if
Possible Duplicate: How to extract a file extension in PHP? I found the following
Possible Duplicate: A PHP regex to extract php functions from code files I have
Possible Duplicate: How can I extract images from a PDF file? I am able
Possible Duplicate: php regex , extract phone number from text/html I have following requirement.
Possible Duplicate: how to extract plain text from ms word document file in pure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.