0

I'm getting a file with "file_get_contents", there is any PHP Function that will give me the type of the file? In this example "jpg".

$imagedata = file_get_contents("/path/to/image.jpg");      

Please give me some clues.

Best Regards,

André
  • 24,706
  • 43
  • 121
  • 178
  • Duuuuplicate: http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php – thirtydot Jan 20 '11 at 16:11
  • **Note:** the **type of file** and the **file extension** are completely different concepts. A .php file might be renamed to .jpg, it would look like a jpg file, but the file type is still php. Whereas you can find the **file extension** using `pathinfo()`, the file type is best found using `fileinfo` functions. – Christian Mar 23 '11 at 10:13

2 Answers2

1

You can use pathinfo like this:

$extension = pathinfo($filename, PATHINFO_EXTENSION); // jpg

Note that since this fetch the extension from the filename, it can easily be manipulated. You should use a function like getimagesize to determine if a file is a real image.

alexn
  • 57,867
  • 14
  • 111
  • 145
1

The word you are looking for is extension.

$ext = pathinfo($file, PATHINFO_EXTENSION);

This is an exact duplicate of How to extract a file extension in PHP? though, try searching a bit better next time.

Community
  • 1
  • 1
orlp
  • 112,504
  • 36
  • 218
  • 315