.rar,.zip文件MIME类型

编程入门 行业动态 更新时间:2024-10-28 00:14:20
本文介绍了.rar,.zip文件MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个简单的php上传脚本,用户只能上传ZIP和RAR文件.

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

我应该使用哪些MIME类型检查$_FILES[x][type]? (请提供完整列表)

What MIME types I should use to check $_FILES[x][type]? (a complete list please)

谢谢..

推荐答案

freedompeace,Kiyarash和Sam Vloeberghs的回答:

The answers from freedompeace, Kiyarash and Sam Vloeberghs:

.rar application/x-rar-compressed, application/octet-stream .zip application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip

我也会检查文件名.这是检查文件是RAR还是ZIP文件的方法.我通过创建一个快速的命令行应用程序对其进行了测试.

I would do a check on the file name too. Here is how you could check if the file is a RAR or ZIP file. I tested it by creating a quick command line application.

<?php if (isRarOrZip($argv[1])) { echo 'It is probably a RAR or ZIP file.'; } else { echo 'It is probably not a RAR or ZIP file.'; } function isRarOrZip($file) { // get the first 7 bytes $bytes = file_get_contents($file, FALSE, NULL, 0, 7); $ext = strtolower(substr($file, - 4)); // RAR magic number: Rar!\x1A\x07\x00 // en.wikipedia/wiki/RAR if ($ext == '.rar' and bin2hex($bytes) == '526172211a0700') { return TRUE; } // ZIP magic number: none, though PK\003\004, PK\005\006 (empty archive), // or PK\007\008 (spanned archive) are common. // en.wikipedia/wiki/ZIP_(file_format) if ($ext == '.zip' and substr($bytes, 0, 2) == 'PK') { return TRUE; } return FALSE; }

请注意,仍不能100%确定,但它可能已经足够好了.

Notice that it still won't be 100% certain, but it is probably good enough.

$ rar.exe l somefile.zip somefile.zip is not RAR archive

但是,即使WinRAR也将非RAR文件检测为SFX存档:

But even WinRAR detects non RAR files as SFX archives:

$ rar.exe l somefile.srr SFX Volume somefile.srr

更多推荐

.rar,.zip文件MIME类型

本文发布于:2023-11-06 13:15:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   文件   rar   zip   MIME

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!