.rar, .zip files MIME Type

后端 未结 6 641
自闭症患者
自闭症患者 2020-11-28 03:52

I\'m developing a simple php upload script, and users can upload only ZIP and RAR files.

What MIME types I should use to check $_FILES[x][type]? (a comp

6条回答
  •  北海茫月
    2020-11-28 04:39

    For upload:

    An official list of mime types can be found at The Internet Assigned Numbers Authority (IANA) . According to their list Content-Type header for zip is application/zip.

    The media type for rar files is not officially registered at IANA but the unofficial commonly used mime-type value is application/x-rar-compressed.

    application/octet-stream means as much as: "I send you a file stream and the content of this stream is not specified" (so it is true that it can be a zip or rar file as well). The server is supposed to detect what the actual content of the stream is.

    Note: For upload it is not safe to rely on the mime type set in the Content-Type header. The header is set on the client and can be set to any random value. Instead you can use the php file info functions to detect the file mime-type on the server.


    For download:

    If you want to download a zip file and nothing else you should only set one single Accept header value. Any additional values set will be used as a fallback in case the server cannot satisfy your in the Accept header requested mime-type.

    According to the WC3 specifications this:

    application/zip, application/octet-stream 
    

    will be intrepreted as: "I prefer a application/zip mime-type, but if you cannot deliver this an application/octet-stream (a file stream) is also fine".

    So only a single:

    application/zip
    

    Will guarantee you a zip file (or a 406 - Not Acceptable response in case the server is unable to satisfy your request).

提交回复
热议问题