Search code examples
phpfile-type

How to get the file type category in php


How could I get the file type category only?

For example:

    $fileType = $_FILES['userfile']['type'];
    echo $fileType;

In this code, it displays image/jpeg. Now what I want to get is only the image without the jpeg.

Same also with all of this:

Displays image only if the file type is:

  • image/png
  • image/gif
  • image/bmp

and displays text only if file type is:

  • text/html
  • text/plain
  • text/css

and so on, with the other file type..

Any help is highly appreciated. Thanks


Solution

  • Ugly, and may not work on all file types:

    $type = explode('/', $filetype)[0];