简体   繁体   English

PHP 获取远程文件 MIME 类型

[英]PHP get remote file mime-type

嘿,检查远程文件 mime 类型的最快方法是什么......我正在考虑从第一个字节中读取一些,也许更多......我花了一些时间思考如何使事情正确,但我什么都没想...我必须检查远程文件是否是mp3,但必须是fastcheck ...

PHP curl_getinfo() PHP curl_getinfo()

<?php
    # the request
    $ch = curl_init('http://www.google.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);

    # get the content type
    echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    # output
    // text/html; charset=ISO-8859-1
?>

output输出

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 09 Apr 2010 20:35:12 GMT
Expires: Sun, 09 May 2010 20:35:12 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219

To actually confirm if the file contains actual MP3 data or any other media format, I use getID3() .为了实际确认文件是否包含实际的 MP3 数据或任何其他媒体格式,我使用getID3()

Make a HEAD request.发出 HEAD 请求。 See what Content-Type the server claims it is.查看服务器声称的内容类型。

This stackoverflow question discusses accessing HTTP headers via PHP. 这个 stackoverflow 问题讨论了通过 PHP 访问 HTTP 标头。

When checking only the Content-Type of the remote server you're trusting it:仅检查您信任的远程服务器的Content-Type时:

  • The remote server could be malicious and serve a wrong Content-Type on purpose远程服务器可能是恶意的,并故意提供错误的内容类型
  • Content-Type is generated using the file extension, so a malicious user could store a wrong file type with an authorized extension to bypass your security Content-Type 是使用文件扩展名生成的,因此恶意用户可以使用授权扩展名存储错误的文件类型以绕过您的安全

A safer approach is download the file in a temporary folder and then use mime_content_type to check the MIME-type locally using magic.mime .更安全的方法是将文件下载到临时文件夹中,然后使用mime_content_type使用magic.mime在本地检查 MIME 类型。

This technique only use the magic number , only reading the first bytes (header) of the file, that can be bypassed.这种技术只使用幻数,只读取文件的第一个字节(头),可以绕过。

The most secure approach is to use a library that verify that the given file is valid for the required type it should be.最安全的方法是使用一个库来验证给定的文件对于它应该是的所需类型是否有效。

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

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