简体   繁体   English

如何从PHP中的'php:// input'获取mime内容类型

[英]How to get the mime content type from 'php://input' in PHP

The images are dropped on an area, and then, have to be automatically saved on server. 图像放置在某个区域,然后必须自动保存在服务器上。

Images are send throw an AJAX request. 发送的图像会引发AJAX请求。

I would like to check the mime type from the server side to be sure I will not save anything unsafe. 我想从服务器端检查mime类型,以确保不会保存任何不安全的内容。

The headers are not really a solution, because they come from the client side, so they can be changed. 标头并不是真正的解决方案,因为它们来自客户端,因此可以更改它们。

PHP PHP

I receive the file like this : 我收到这样的文件:

$source = file_get_contents('php://input');

I tried to get the mime type with 我试图获得哑剧类型

mime_content_type($filename);

and

finfo_file($finfo, $filename);

but what is $filename in my case ? 但是在我的情况下$ filename是什么? It doesn't work with 它不适用于

$filename = 'php://input';

Can I get the mime content type from a different way ? 我可以通过其他方式获得mime内容类型吗?

AJAX AJAX

addEvent(canvas, 'drop', function(e) {
    e.preventDefault();
    var files = e.dataTransfer.files;
    upload(files,e.target,0);
});

function upload(files, area, index){
    var file = files[index];
    xhr.open("post", "/index.php", true);
    xhr.setRequestHeader("content-type", "multipart/form-data");
    xhr.setRequestHeader("X-File-Type", file.type);
    xhr.setRequestHeader("X-File-Size", file.size);

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
            read(xhr.responseText);
        }
    }

    xhr.send(file);
}

Any idea ? 任何想法 ?

Thank you Cyclone, I did this: 谢谢Cyclone,我做到了:

To save in a temporary file : 要保存在临时文件中:

$temp_file = tempnam(sys_get_temp_dir(), 'Tux');  
file_put_contents($temp_file , $source); 

To get the mime content type : 要获取mime内容,请输入:

$finfo = finfo_open(FILEINFO_MIME_TYPE);  
$content_type = finfo_file($finfo, $temp_file);

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

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