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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:27:13+00:00 2026-06-14T17:27:13+00:00

I need to find what kind of file user have upload by checking binary

  • 0

I need to find what kind of file user have upload by checking binary data, and I found perfect solution for that, over here

Just to be specific this is the function I’m using:

function getImgType($filename) {
    $handle = @fopen($filename, 'r');
    if (!$handle)
        throw new Exception('File Open Error');

    $types = array('jpeg' => "\xFF\xD8\xFF", 'gif' => 'GIF', 'png' => "\x89\x50\x4e\x47\x0d\x0a", 'bmp' => 'BM', 'psd' => '8BPS', 'swf' => 'FWS');
    $bytes = fgets($handle, 8);
    $found = 'other';

    foreach ($types as $type => $header) {
        if (strpos($bytes, $header) === 0) {
            $found = $type;
            break;
        }
    }
    fclose($handle);
    return $found;
}

Now my question is, how can I get bits for other file types, like .zip, .exe, mp3, mp4 etc… if there is some kind of list somewhere out there it would be great, though I would like to extract it myself and learn how all of this really works.

  • 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-14T17:27:15+00:00Added an answer on June 14, 2026 at 5:27 pm

    What you’re looking for is called file magic number.

    The magic number is a type of file signature – since sometimes it takes more than the magic number to identify the file.

    A (very) short list of such numbers can be found here. A larger list can be found here.

    File identification websites often times also mention the file magic number.

    In linux, the file command can be used to identify files.
    In PHP you can use the FileInfo set of functions to identify files.


    By the way, you did not specify the kind of files you want to identify. Sometimes, identification might be the wrong solution. For example, people used to want to identify files before passing them to GD or saving them on the server as images.
    In this case, identification is not really your job. Instead, use the following code:

    $data = file_get_contents('data.dat'); // File might eventcontain a JPG...it is
                                           // still loaded without problems!
    $image = imagecreatefromstring($data); // ... since this function just needs the
                                           // file's data, nothing more.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find a silence in a mp3 file. Simple as that. For
I need to find a flexible solution for access control in PHP. In the
I need to find all the occurences of a file Test.TXT in c:\ or
I need to find the longest sequence in a string with the caveat that
I have a need to have a config.xml file in my Eclipse project, and
Being a programmer I occasionally find the need to analyze large amounts of data
I have a find function that locates a string in a JTable with quite
I need to store the application settings somewhere, but can't find a satisfying solution.
I have a daemon that reads a configuration file in order to know where
If I need to have scripts like below: find -name 'lib*.so' | xargs cp

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.