如何在Zend Framework 2模型声明中添加File \ MimeType Validator

时间:2012-12-06 17:13:57

标签: php validation zend-form zend-framework2 zend-validate

其实我正在开发一个简单的文件上传器。

在ImageUploader.php文件中我定义了getInputFilter函数,除非我尝试添加File \ MimeType验证器,否则一切正常:

<?php

namespace Admin\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

use Zend\Validator\File\MimeType; //tried also with use Zend\Validator\File;

[...]
public function getInputFilter()
{
    [...]
    $inputFilter->add($factory->createInput(array(
       'name'     => 'image',
       'required' => true,
       'validators' => array(
            array(
                'name'    => 'MimeType', //tried also with File\MimeType
                'options' => array(
                   'mimeType' => array('image/jpeg'),
                ),
            ),
        ),
    )));
    [...]
}

定义File \ Validator \ MimeType的正确方法是什么?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

以下是我项目中的一些示例,其中包含其他一些有用的功能(例如重命名):

use Zend\File\Transfer\Adapter\Http,
    Zend\Validator\File\Size,
    Zend\Validator\File\IsImage;

[...]
public function editAction() {    
[...]
  if ($request->isPost()) {
    $params = $this->params()->fromPost();
    $files = $this->params()->fromFiles();

    $images = array();
    if(!empty($files['main_image']['name'])) $images['main_image'] = $files['main_image']['name'];
    if(!empty($files['detailed_image']['name'])) $images['detailed_image'] = $files['detailed_image']['name'];

    if(!empty($images)) {
      $adapter = new Http();
      $size = new Size(array('max'=>1000000));
      $is_image = new IsImage();
      $image_errors = array();
      foreach ($images as $field => $filename) {
        $adapter->setValidators(array($size,$is_image), $filename);
        if (!$adapter->isValid($filename)) {
          $adapter_errors = $adapter->getMessages();
          $errors = array();
          foreach($adapter_errors as $key=>$row) {
            $errors[] = $row;
          }
          if(!empty($errors)) {
            array_push($image_errors, $errors);
            $form->setMessages(array($field => $errors));
          }
        } else {
          $adapter->setDestination(IMAGES_PATH);
          $fileinfo = $adapter->getFileInfo();
          preg_match('/.+\/(.+)/', $fileinfo[$field]['type'], $matches);
          $extension = $matches[1];
          $old_filename = $item->__get($field);
          $new_filename = $item->id.'_'.$field.'.'.$extension;

          $adapter->addFilter('File\Rename',
            array(
              'target' => IMAGES_PATH.$new_filename,
              'overwrite' => true,
            )
          );

          if($adapter->receive($fileinfo[$field]['name'])) {
            if(!empty($old_filename) && $old_filename != $new_filename &&
              file_exists(IMAGES_PATH.$old_filename)) {
              unlink(IMAGES_PATH.$old_filename);
            }
            $params[$field] = $new_filename;
          }
        } 
      }
    }
  }
[...]
}
[...]

然而,可以重构代码以将逻辑移动到某些lib或模型。

答案 1 :(得分:0)

不确定您现在使用的是哪个版本,但使用Zendframework 2.4,您现在可以执行此操作。希望这会有所帮助。

public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();

        $inputFilter->add(array(
            'name'     => 'fileupload',
            'required' => true,
            'allow_empty' => false,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'Zend\Validator\File\UploadFile',
                    'name' => 'Zend\Validator\File\MimeType',
                        'options' => array(
                            'mimeType' => 'audio, video, image',
                        ),
                    ),
                ),
        ));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
 }