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

  • Home
  • SEARCH
  • 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 7958441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:25:25+00:00 2026-06-04T04:25:25+00:00

I want to validate file types in server using php. Now at the moment

  • 0

I want to validate file types in server using php. Now at the moment I have validated it by checking the file extension type of the file. This works fines but the problem is that this isn’t the best way to validate the file as a user an change lets say a text file into a jpeg and still be able to upload it.

So what I want to do is add another validation method, I also want to check the file’s MIME type using php to recognize if the file is a image, video or audio.

So my question is how is it coded so that I can use MIME type in php to be able to validate a file? Also if I have an extremely large file, does it take a long time for the MIME validation to kick in or does it validate straight away?

Below is currently the code I have for Image only where it uses php code to check for file type:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
  • 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-04T04:25:25+00:00Added an answer on June 4, 2026 at 4:25 am

    MIME type lookup is usually pretty fast, and in PHP you can do it with the finfo extension. Example:

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mimetype = finfo_file($finfo, '/path/to/file.jpg'); // image/jpeg
    finfo_close($finfo);
    

    Also, you shouldn’t rely on the type index of the $_FILES superglobal, as that value can be spoofed to anything an attacker may want. Same thing goes for the file extension of course.

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

Sidebar

Related Questions

I have been writing a file uploader, and I want to validate the types
hello everyone i want validate the type of file being uploaded on sever to
I have a form that I want to validate looks like this: <!DOCTYPE html
I know how to validate this client and server side by extension and MIME
I want to validate a file uploader, by file extension. If the file extension
I have an input, type text, element which is being validated using MVC3 validation
I want to validate the file type to make sure the user is uploading
I want to validate an XML file against an XML Schema file. It is
Very basic question- I've a xml file and I want to validate it against
I want to validate array of beans using JSR 303 Validation. Like spec says,

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.