简体   繁体   English

为什么PHP没有为json文件返回正确的mime?

[英]Why is PHP not returning correct mime for json files?

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? 我知道json文件的正确MIME类型是applicattion/json ,可以通过这篇文章确认什么是正确的JSON内容类型? .

However, I wonder why my PHP installation is returning text/plain . 但是,我想知道为什么我的PHP安装返回text/plain I need to test for the correct mime before parsing the json file. 我需要在解析json文件之前测试正确的mime。

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 . 其中$fileType返回text/plain而不是applicattion/json

Again, $_FILES['type'] returns application/octet-stream instead of applicattion/json . 同样, $_FILES['type']返回application/octet-stream而不是applicattion/json

What am I missing? 我错过了什么?

Edit 编辑

I am sending the file through jQuery ajax: 我通过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,
})

I'm not exactly sure what your actual problem is. 我不确定你的实际问题是什么。 It refers to two ways to obtain some MIME type. 它指的是两种获取某种MIME类型的方法。

Fileinfo uses libmagic. Fileinfo使用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 ie begins with GIF89a it will report image/gif. 如果它以GIF89a开头,它将报告image / gif。 The guess is often wrong but can be enough. 猜测通常是错误的,但可能就足够了。

$_FILES contains information the client (web browser) is sending. $_FILES包含客户端(Web浏览器)正在发送的信息。 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. 如何执行此操作取决于文件的来源以及您打算执行的操作。 ie 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 (ie to get rid of exif data) 对于从不太受信任的用户上传的图像(我希望你不打算接受从不完全信任的用户上传的javascript文件来执行),一个好方法是实际尝试打开图像,甚至可能重新编码它(即摆脱exif数据)

finfo identifies file type by it content, not file extension. finfo通过内容识别文件类型,而不是文件扩展名。

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. 否则,将根据其text/plain将其标识为text/plain (ASCII)或application/octet-stream (Binary)。

Unfortunately JSON encoded content has no signature and hence being id-ed as text/plain or application/octet-stream . 不幸的是,JSON编码的内容没有签名,因此被标记为text/plainapplication/octet-stream

For further reading, please visit:- 如需进一步阅读,请访问: -

List of file signatures 文件签名列表

Hope this helps. 希望这可以帮助。

[Edit 1] Below is my test script for the above. [编辑1]以下是我上面的测试脚本。 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);

如果您使用的是* nix系统,则可以始终尝试使用file命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM