使用Laravel 4文件上传mime类型验证

rya*_*cey 4 php file-upload mime-types laravel laravel-4

当我上传一个结构良好的MP3文件,Laravel 4告诉我,这不audio/mp3,但是application/octet-stream,这使得这种验证失败:

$validator = Validator::make(
    array('trackfile' => Input::file('trackfile')), 
    array('trackfile' => 'required|mimes:mp3')
);

if($validator->fails())
    return 'doesn\'t works because mime type is '.Input::file('trackfile')->getMimeType();
else
    return 'it works!';
Run Code Online (Sandbox Code Playgroud)

为什么不将文件上传为audio/mp3文件?

(我已添加'files' => true到表单声明中)

rya*_*cey 11

编辑

内置验证器仍在考虑使用某些mp3作为应用程序/八位字节流,您是否使用此软件转换原始文件.

我终于使用了用户ShaneMimeReader类,它完全有效.我仍然想知道为什么如此难以检测到正确的mime类型.

我在Laravel中将其实现为验证器:

  • 移动MimeReader.phpsapp/libraries/MimeReader.php(看扩展)
  • 扩展Laravel Validator(我把它放在我的BaseController构造函数中)

    Validator::extend('audio', function($attribute, $value, $parameters)
    {
        $allowed = array('audio/mpeg', 'application/ogg', 'audio/wave', 'audio/aiff');
        $mime = new MimeReader($value->getRealPath());
        return in_array($mime->get_type(), $allowed);
    });
    
    Run Code Online (Sandbox Code Playgroud)
  • 添加你的错误信息app/lang/[whatever]/validation.php(在我的情况下,关键是audio)
  • 我现在可以使用audio了!示例:'file' => 'required|audio',where file指的是Input::file('file')

Laravel正在考虑audio/mpeg的文件是.mpga.mp3.我在MimeTypeExtensionGuesser.php(在Symfony库中)纠正了它,并将audio/ogg其视为.oga.也许音频mime类型取决于使用的软件编码器.

其他方法是绕过验证器并使用Input::file('upload')->getMimeType()Sheikh Heera所说的方式将mime-type直接检入控制器.


The*_*pha 5

这可能是因为mp3文件的mime类型是audio/mpeg或者可能是,它无法识别mime_content_type并告诉它application/octet-stream作为通用/后备类型.您可以尝试此规则(下面给出的示例)(不测试/确定),因为根据rfc3003audio/mpeg是正确的mime类型:mp3

array('trackfile' => 'required|mimes:audio/mpeg,mp3')
Run Code Online (Sandbox Code Playgroud)

此外,您可以使用以下内容手动检查mime类型:

$file = Input::file('upload');
if($file->getMimeType() == 'audio/mpeg') {
    // valid
}
else {
    // invalid
}
Run Code Online (Sandbox Code Playgroud)

更新:

特别地,对于mp3文件,你也可以使用一个库getID3这是类似PHP的PECL扩展,ID3,如果你使用它,你也将能够使用ID3 tag的文件,并可以得到的信息,如title,artist,album,year,genre等.

如果你使用它,你必须从给定的链接(getid3)手动下载它并将其放在一个文件夹里面app,也许app/libs/只需将getid3提取的源libs文件夹放入文件夹然后在你的composer.json文件中添加一个条目classmap,autoload如:

// ...
"app/tests/TestCase.php",
"app/libs/getid3"
Run Code Online (Sandbox Code Playgroud)

然后composer dump-autoload从终端/命令提示符运行,您就可以使用它了.要使用它,请尝试以下方法:

$file = Input::file('upload'); // assumed name of the file input is upload
$path = $path = $file->getRealPath();
$id3 = new getID3();
$tags = $id3->analyze($path);
if(is_array($tags) && array_key_exists('audio', $tags)) {
    // valid
    dd($tag['tags']);
}
Run Code Online (Sandbox Code Playgroud)

对于有效mp3文件,您将获得如下数组:

array (size=2)   'id3v1' => 
    array (size=6)
      'title' => 
        array (size=1)
          0 => string '46. Piya Basanti' (length=16)
      'artist' => 
        array (size=1)
          0 => string '(Freshmaza.com)' (length=15)
      'album' =>
      ...
Run Code Online (Sandbox Code Playgroud)

如果您已经安装或可以PECL使用(从终端)以下命令安装扩展:

pear install pecl/id3
Run Code Online (Sandbox Code Playgroud)

那你通常可以使用

$tags = id3_get_tag( "path/to/example.mp3" );
print_r($tag);
Run Code Online (Sandbox Code Playgroud)

上面的例子将输出类似于:

Array
(
    [title] => DN-38416
    [artist] => Re:\Legion
    [album] => Reflections
    [year] => 2004
    [genre] => 19
)
Run Code Online (Sandbox Code Playgroud)

检查Php手册.