MIME,八位字节流和Uploadify [英] MIMEs, octet-stream and Uploadify

查看:242
本文介绍了MIME,八位字节流和Uploadify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Kohana ,我正在创建文件上传。用户只能上传几种类型的文件。



Kohana内置了很多MIME类型的库。我认为这将是很酷,检查MIME类型的上传文件(它来自Uploadify)匹配设置的文件扩展名。这就是为什么我做了一个允许MIME类型的数组。

  $ mimes =(array)Kohana :: config('mimes') ; 
$ allowed_mimes = array_merge($ mimes ['bmp'],$ mimes ['gif'],$ mimes ['jpg'],$ mimes ['jpeg'],$ mimes ['png']);

接下来,我想检查上传的文件MIME类型是在 $ allowed_mimes 数组。我用了 in_array($ file ['type'],$ allowed_mimes)之类的东西。让我吃惊的是,实际的文件MIME是 application / octet-stream 。无论如何,上传的文件是 JPEG 图片。这是怎么可能的?



基本思想是我需要检查文件类型。什么是最好的方式来做到这一点?



编辑:

和同事们一起,我决定在最后一个小点之后再查一下字符。像 virus.jpeg 是可以接受的,因为 jpeg 是它的名字。我仍然打开更好的解决方案!
$ b $扩展= ltrim(strrchr($ file ['name'],'。'), '。')

解决方案

PHP可以使用 fileinfo MIME Magic (已经从PHP 5.3.0中删除)来确定文件的MIME类型(Kohanas也是这样的 File :: mime() / code>方法)。



如果这两个都不可用,这个方法将尝试使用文件的扩展名来查找MIME类型,这可能是非常不可靠的。 b
$ b

由于您只是想验证一些上传,我建议使用上传方法来验证它:



$ $ p $ $ validation = Validation :: factory($ _ FILES)
- >规则('Filedata','Upload :: not_empty')
- >规则('Filedata','Upload :: valid')
- >规则('Filedata','Upload :: size',array(':value','4M') )
- >规则('Filedata','Upload :: type',array(':value',array('bmp','jpg','jpeg','png')))
- >规则('Filedata','Upload :: image',array(':value',1920,1080));

注意 Upload :: image()从3.2.0开始可用(您也可以将其导入到旧版本)。这是我个人使用Uploadify上传的验证,所以它应该工作得很好。


I'm using Uploadify and Kohana and I'm creating file uploader. User can upload only few types of files.

Kohana have great library of MIME types built-in. I thought that it would be cool to check that MIME type of uploaded file (it came from Uploadify) match setted file extensions. That's why I made an array of allowed MIME types.

$mimes         = (array) Kohana::config('mimes');
$allowed_mimes = array_merge($mimes['bmp'], $mimes['gif'], $mimes['jpg'], $mimes['jpeg'], $mimes['png']);

Next, I wanted to check that uploaded files MIME type is in $allowed_mimes array. I used something like in_array($file['type'], $allowed_mimes). For surprise to me - actual MIME of file was application/octet-stream. Anyway, uploaded file was JPEG image. How this is possible?

Basic idea is that I need to check file type. What's the best way to do it?

Edit:

After some conversions with my colleagues, I decided to check chars after last dot. Like virus.jpeg is acceptable, because of jpeg is in its name. i'm still open for better solutions!

$extension = ltrim(strrchr($file['name'], '.'), '.')

解决方案

PHP can use fileinfo and MIME Magic (has been removed from PHP 5.3.0) to determine files' MIME type (and so does Kohanas' File::mime() method).

In case that none of these 2 is available, this method will try to find the MIME type using files' extension, which can be highly unreliable.

Since you are only trying to validate some upload, I'd suggest using Upload methods to validate it:

$validation = Validation::factory($_FILES)
    ->rule('Filedata', 'Upload::not_empty')
    ->rule('Filedata', 'Upload::valid')
    ->rule('Filedata', 'Upload::size',  array(':value', '4M'))
    ->rule('Filedata', 'Upload::type',  array(':value', array('bmp','jpg','jpeg','png')))
    ->rule('Filedata', 'Upload::image', array(':value', 1920, 1080));

Notice that Upload::image() is available since 3.2.0 (you can import it into older versions as well). This is the validation I'm personally using for some Uploadify upload, so it should work just fine.

这篇关于MIME,八位字节流和Uploadify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆