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 3312570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:00:40+00:00 2026-05-17T22:00:40+00:00

Yes as the title says: How can i detect an file extension? $_POST[‘fname’]; is

  • 0

Yes as the title says:

How can i detect an file extension? $_POST[‘fname’]; is where i have the filename stored e.g asddsa.jpg

So how can i check if .jpg then … if .png then … ?

    $src = "images/status/photo/".$_POST['fname'];
    $parts=pathinfo($src);
if($parts['extension'] == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($parts['extension'] == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($parts['extension'] == "gif"){
    $img_r = imagecreatefromgif($src);
}

tried this too without any success:

    $ext= pathinfo($src, PATHINFO_EXTENSION);
if($ext == "jpg"){
    $img_r = imagecreatefromjpeg($src);
}elseif($ext == "png"){
    $img_r = imagecreatefrompng($src);
}elseif($ext == "gif"){
    $img_r = imagecreatefromgif($src);
}
  • 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-05-17T22:00:40+00:00Added an answer on May 17, 2026 at 10:00 pm

    The proper way to do this would be to use pathinfo on the file. For example…

    $parts=pathinfo('somefile.jpg');
    echo $parts['extension']; //Returns "jpg"
    

    Now, you’re simply looking at a string in a variable, so the way to handle this would be something like this…

    $parts=explode(".", $_POST['fname']);
    echo $parts[count($parts)-1];
    

    Finally, you should not be detecting file type by extension. This is bad. Instead, you should be using the content-type (commonly referred to as MIME type). You can find this in $_FILES['userfile']['type'] for uploaded files. (documentation)

    Once you have your type, you can do a simple switch statement.

    Also, beware of folks uploading content that isn’t what it says it is. Someone can send files to you with a specific content type, but it may be something such as executable code instead! The only way to be sure is to check the files with your own code server-side. Never trust user input.

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

Sidebar

Related Questions

Crappy title, yes. But I honestly have no idea what this particular line of
As the title says, here's some code: - (void)refreshMap { NSLog(@refreshing); [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
The title pretty much says it all. If I have something like (from PHP
As the title says everything, If the answer is yes then I would like
Yes, the title doesn't make much sense, but here's my situation. I have two
as already the title of my question says I have the problem that the
Yes I know, this title isn't really helpfull but this is the exact problem.
Generally, message boxes work like this: if(ShowMessageBox(Title,Text,MB_YES_NO) == MB_YES) { //the user responded yes
Yes, I have searched and tried many techniques, but nothing seems to work. Here
Yes, I know this question has been asked before, but I can't find an

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.