0

我有一个文件上传功能,我想允许图像和文档并禁止其他文件,只检查扩展名听起来是个坏主意。

我拥有的代码片段是:

var $actsAs = array(
    'MeioUpload' => array(
        'file' => array(
            'dir' => 'uploads{DS}{ModelName}',
            'fields' => array(
                'filesize' => 'size',
                'mimetype' => array('image/jpeg', 'image/pjpeg', 'image/gif',
                'image/png', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'application/vnd.openxmlformats-officedocument.presentationml.presentation','application/pdf'),
                'message' => 'Please upload a valid document file'
            ),
            'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif',
                'image/png', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'application/vnd.openxmlformats-officedocument.presentationml.presentation','application/pdf'),
            'allowed_ext' => array(' .docx',' .jpg',' .jpeg',' .png',' .gif',' .doc',' .xls',' .xlsx',' .pdf'),
            'validations' => array(
                'MaxSize' => array(
                    'check' => false,
                ),
            ),
            'message' => 'Invalid File. Please upload only document or images'
        ),
    ),
);

如何分配 mimetypes 以允许:.txt、.doc、.docx、.xls、.xlsx、.pdf、.png、.jpg、.gif、.jpeg

MEIOUPLOAD 调整会很棒!

4

2 回答 2

1

尝试了几个位置,最后求助于核心 PHP 功能。MeioUpload 不支持检查多种 mime 类型。希望未来版本中可能会出现增强功能。

                $allowMime = array('image/jpeg', 'image/pjpeg', 'image/gif',
                                'image/png', 'application/msword','application/vnd.ms-office','application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                                'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
                                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                                'application/vnd.openxmlformats-officedocument.presentationml.presentation','application/pdf');
                $file_info = new finfo(FILEINFO_MIME);
                $filename = $attachments[$file_name];
                $data = explode(";",$file_info->file($filename));
                $mime = $data[0];
                if(in_array($mime, $allowMime))
                {
 //Your code here
于 2012-08-28T06:57:25.603 回答
0

您可以使用Mime 类型模型验证。这是代码:

This rule checks for valid mimeType

<?php
   public $validate = array('image' => array(
                      'rule' => array('mimeType', array('image/gif')),
                      'message' => 'Invalid mime type.'
                                    ),
                            );
于 2012-08-17T10:01:59.193 回答