4

I am aware that the correct MIME type fo json files is applicattion/json as may be confirmed by this post What is the correct JSON content type?.

However, I wonder why my PHP installation is returning text/plain. I need to test for the correct mime before parsing the json file.

I have the following code

$fileinfo = new finfo();
$fileType = $fileinfo->file( $_FILES['tmp_name'], FILEINFO_MIME_TYPE );

in which $fileType returns text/plain instead of applicattion/json.

Again, $_FILES['type'] returns application/octet-stream instead of applicattion/json.

What am I missing?

Edit

I am sending the file through jQuery ajax:

var formData = new FormData( $(form)[0] );
var jsonFile = $( 'input:file[name=contents]', form ).get(0).files[0];

formData.append( 'jsonFile', jsonFile );

$.ajax({ 

    type: 'POST',
    url: 'url',
    data: formData,
    dataType:'json',
    enctype : 'multipart/form-data',
    processData: false,
    contentType : false,
    encode:true,
})
Community
  • 1
  • 1
Stephen Adelakun
  • 694
  • 2
  • 6
  • 23

3 Answers3

4

I'm not exactly sure what your actual problem is. It refers to two ways to obtain some MIME type.

Fileinfo uses libmagic. As the name indicates there is magic happening here. Essentially it looks at the file and tries to guess what type a file might be. If it i.e. begins with GIF89a it will report image/gif. The guess is often wrong but can be enough.

$_FILES contains information the client (web browser) is sending. the type in there is what the browser things. This often is completely useless.

If you need a precise type you have to ensure this yourself. How to do this depends on where the file is coming from and what you plan to do. i.e. if this comes from a trustworthy admin you might look at the file extension. For images uploaded from less trusted users (I hope you're not planning to accept javascript files uploaded from not fully trusted users to be executed) a good way is to actually trying to open the image and maybe even re-encoding it (i.e. to get rid of exif data)

johannes
  • 15,041
  • 3
  • 39
  • 56
  • I am not sure either that you read my question clearly. The correct mime type for json file is `application/json` I need to test for this, so why is PHP not returning this despite saving the file with `.json`, the file containing well-formed json content and using different methods for the test returns `text/plain`. – Stephen Adelakun Aug 23 '16 at 11:51
4

finfo identifies file type by it content, not file extension.

Only file with signature could be identified properly. Otherwise, will be id-ed as either text/plain (ASCII) or application/octet-stream (Binary) based on it's content.

Unfortunately JSON encoded content has no signature and hence being id-ed as text/plain or application/octet-stream.

For further reading, please visit:-

List of file signatures

Hope this helps.

[Edit 1] Below is my test script for the above. Cheers.

$filename = "test.json";

$finfo = finfo_open(FILEINFO_MIME_TYPE);

file_put_contents($filename, "<?php \n");
printf("%s\n", finfo_file($finfo, $filename));

file_put_contents($filename, "@echo off\n");
printf("%s\n", finfo_file($finfo, $filename));

file_put_contents($filename, json_encode(array("a" => "1")));
printf("%s\n", finfo_file($finfo, $filename));

file_put_contents($filename, "\xff");
printf("%s\n", finfo_file($finfo, $filename));

finfo_close($finfo);
Capital C
  • 309
  • 2
  • 12
  • It is very common for answerers on SO to jump at the OP and write-off without patience to actually understand what the OP wants. This one is an exception. – Stephen Adelakun Aug 23 '16 at 12:22
0

If you are on a *nix system, you could always try to use the file command.

muchar
  • 59
  • 6