2017-07-10 25 views
0

现在我对这个ZF3的东西有点更多了。我可以(在一些帮助下)实现我想要的几乎所有东西。为了探索新的版本,我开发了一个测试项目。fileupload的输入过滤器,基本用法

有些问题仍然没有答案,我没有找到可用的解释。

我的新问题是InputFilterAwareInterface。我尝试了教程中的字符串示例,到目前为止一切正常。但总是为了轻松的主题找到一切,如果你走得更远,它会突然结束。

我需要用于xls和xlsx文件的Inputfilter。我当然搜索了一下,阅读教程,在zend教程中搜索,因为我有这个想法,必须有某个地方存在一些完整的参考,但我找不到任何。

所以,我想这一个:

$inputFilter->add([ 
       'type'  => 'Zend\InputFilter\FileInput', 
       'name'  => 'DCL_Path', 
       'required' => true, 
       'validators' => [ 
         ['name' => 'FileUploadFile'], 
         [ 
           'name' => 'FileMimeType', 
           'options' => [ 
             'mimeType' => ['text/xls', 'text/xlsx'] 
           ] 
         ], 
         [ 
           'name' => 'Filesize', 
           'options' => [ 
             'max' => 4096 
           ] 
         ], 
       ], 
//    'filters' => [ 
//      [ 
//        'name' => 'FileRenameUpload', 
//        'options' => [ 
//          'target'=>'./data/upload', 
//          'useUploadName'=>true, 
//          'useUploadExtension'=>true, 
//          'overwrite'=>true, 
//          'randomize'=>false 
//        ] 
//      ] 
//    ], 
     ]); 

正如你看到的,我仍然战斗验证的一部分。验证xls和xlsx文件的正确语法是什么,最大容量是4MB?

而在这之后,有关filterarea,我做了以下在我的控制器动作,只是因为我已经习惯了

if ($form->isValid()) { 
        $data = $form->getData(); 
        // Upload path 
        $location = "public/files/"; 
        // A bit validation of uploaded file 
        $allowedExtension = array('xls', 'xlsx'); 
        $extension = explode('.', $data['DCL_Path']['name']); 
        $extension = end($extension); 
        //$import['DCL_Path']=$data['DCL_Path']['name']; 
        //$fileName = time() . '.' . $extension; 
        $fileName = $data['DCL_Path']['name']; 
        // Check if everything is OK! 
        //echo $fileName; 
        if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) { 
         move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName); 
        } else { 
         echo 'Something went wrong!'; 
        } 

是对move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName); obsolet在界面上filterstuff?在这种情况下,语法又将如何?

而我最大的心愿之一,也有人知道怎样的一个教程这也解释清楚的两个选项(验证器和过滤器)的各种可能性和钥匙的?有时我不相信你需要很多时间才能找到正确的钥匙。

编辑1:节目形态,filterstuff和变更控制

这里是部分我的Form类:

<?php 
namespace Import\Form; 

use Zend\Form\Form; 
class ImportForm extends Form 
{ 
    public function __construct($name = null) 
    { 
     // We will ignore the name provided to the constructor 
     parent::__construct('import'); 

     $this->add([ 
       'name' => 'DCLID', 
       'type' => 'hidden', 
     ]); 
     $this->add([ 
       'name' => 'UnitID', 
       'type' => 'text', 
       'options' => [ 
         'label' => 'equipment', 

       ], 
     ]); 
     $this->add([ 
       'name' => 'DCL_Path', 
       'type' => 'File', 
       //'required' => true, 
       'options' => [ 
         'label' => 'path to file', 

       ], 
       //    'name' => 'FileRenameUpload', 
//    'filters' => [ 
//      'target'=>'./public/files', 
//      'useUploadName'=>true, 
//      'useUploadExtension'=>true, 
//      'overwrite'=>true, 
//      'randomize'=>false 
//     ], 
//    'validators' => [  // Validators. 
//      // Put validator info here. 
//    ] 
     ]); 

这里类的一部分延伸InputFilterAwareInterface

<?php 
namespace Import\Model; 
use DomainException; 
use Zend\Filter\StringTrim; 
use Zend\Filter\StripTags; 
use Zend\Filter\ToInt; 
use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\InputFilterAwareInterface; 
use Zend\InputFilter\InputFilterInterface; 
use Zend\Validator\StringLength; 

class Import implements InputFilterAwareInterface 
{ 
    public $DCLID; 
    public $DCL_Path; 
    public $Unitname; 
    public $UnitID; 
    public $Importdate; 
    public $Importuser; 
    public $Importok; 
    public $DCL_Type; 
    public $Changed_per_User; 
    public $Description_Changes; 

    private $inputFilter; 

    public function exchangeArray(array $data) 
    { 
     $this->DCLID= !empty($data['DCLID']) ? $data['DCLID'] : null; 
     $this->UnitID= !empty($data['UnitID']) ? $data['UnitID'] : null; 
     $this->DCL_Path= !empty($data['DCL_Path']) ? $data['DCL_Path'] : null; 
     $this->Importdate= !empty($data['Importdate']) ? $data['Importdate'] : null; 
     $this->Importuser= !empty($data['Importuser']) ? $data['Importuser'] : null; 
     $this->Importok= !empty($data['Importok']) ? $data['Importok'] : null; 
     $this->DCL_Type= !empty($data['DCL_Type']) ? $data['DCL_Type'] : null; 
     $this->Changed_per_User= !empty($data['Changed_per_User']) ? $data['Changed_per_User'] : null; 
     $this->Description_Changes= !empty($data['Description_Changes']) ? $data['Description_Changes'] : null; 
    } 

    public function getArrayCopy() 
    { 
     //  echo var_dump(get_object_vars($this) 
     //    ); 

     return get_object_vars($this); 
    } 

    public function setInputFilter(InputFilterInterface $inputFilter) 
    { 
     throw new DomainException(sprintf(
       '%s does not allow injection of an alternate input filter', 
       __CLASS__ 
       )); 
    } 

    public function getInputFilter() 
    { 
     if ($this->inputFilter) { 
      return $this->inputFilter; 
     } 

     $inputFilter = new InputFilter(); 

//  $inputFilter->add([ 
//    'name' => 'DCLID', 
//    'required' => false, 
//    'filters' => [ 
//      ['name' => ToInt::class], 
//    ], 
//  ]); 
     // Validator für das Upload Element 

     $inputFilter->add([ 
       'type'  => 'Zend\InputFilter\FileInput', 
       'name'  => 'DCL_Path', // Element's name. 
       'required' => true, // Whether the field is required. 
       'filters' => [  // Filters. 
         [ 
           'name' => \Zend\Filter\File\RenameUpload::class, 
           'options' => [ 
             'use_upload_extension' => true, 
             'randomize' => false, 
             'overwrite' => true, 
             'target' => 'public/files', 
           ], 
         ], 
       ], 
       'validators' => [  // Validators. 
         [ 
           'name' => \Zend\Validator\File\Extension::class, 
           'options' => [ 
             'extension' => 'xls, xlsx', 
             'message' => 'File extension not match', 
           ], 
         ], 
         [ 
           'name' => \Zend\Validator\File\MimeType::class, 
           'options' => [ 
             'mimeType' => 'text/xls', 'text/xlsx', 
             'message' => 'File type not match', 
           ], 
         ], 
         [ 
           'name' => \Zend\Validator\File\Size::class, 
           'options' => [ 
             'min' => '1kB', // minimum of 1kB 
             'max' => '4MB', 
             'message' => 'File too large', 
           ], 
         ], 
       ] 
     ]); 

和我的一部分我的控制界限,我认为可能是问题,有些东西可能不是逻辑:

$form = new ImportForm(); 
     $form->get('submit')->setValue('Add'); //Änderung des LAbels des Submit Buttons, um das Form wiederverwenden zu können 
     //echo "hier"; 
     $request = $this->getRequest(); 
     if (! $request->isPost()) {  //wurden Daten über POST geschickt? 
      return ['form' => $form]; //Keine Daten, nur Form anzeigen, nicht verarbeiten 
     } 
     else { 
     //Es wurden Daten gesendet 
      //echo "Daten"; 
      $import = new Import();  //Neue Instanz von Import 
      $form->setInputFilter($import->getInputFilter());  //Filter an Form binden 
      $form->setData($request->getPost());  //Daten abholen 
      //echo $form->isValid(); 
      if (! $form->isValid()) { 
       return ['form' => $form];  //Wenn die Daten nicht valide sind 
      } 
      else{   //aus Tableadapter 
        $import->exchangeArray($form->getData()); 
        $data = array_merge_recursive(
          $this->getRequest()->getPost()->toArray(), 
          $this->getRequest()->getFiles()->toArray() 
          ); 
        $form->setData($data); 
        if ($form->isValid()) { 
         $data = $form->getData(); 
         // Upload path 
       //  $location = "public/files/"; 
//      $allowedExtension = array('xls', 'xlsx'); 
//      $extension = explode('.', $data['DCL_Path']['name']); 
//      $extension = end($extension); 
         $fileName = $data['DCL_Path']['name']; 
//      // Check if everything is OK! 
//      //echo $fileName; 
//      if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) { 
//       move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName); 
//      } else { 
//       echo 'Something went wrong!'; 
//      } 

         //----------------------------------------------------------------- 
         // t_dcl befüllen 
         //----------------------------------------------------------------- 
         //$namen = explode(",", $import); //Konvertierung des Strings in ein Array 
         //echo "<pre>"; var_dump($namen); echo "</pre>"; //Formartierte Ausgabe des Arrays 
         $this->table->saveImport($import); 

EDIT2:controlleraction的后部分讨论为了一些语句:

controlleraction

$form = new ImportForm(); 
     $form->get('submit')->setValue('Add'); 
     $request = $this->getRequest(); 
     if (! $request->isPost()) {  
      return ['form' => $form]; 
     } 
     else { 
      $import = new Import();  //Neue Instanz von Import 
      $form->setInputFilter($import->getInputFilter());  
      $form->setData($request->getPost());   
      $data = array_merge_recursive(
        $this->getRequest()->getPost()->toArray(), 
        $this->getRequest()->getFiles()->toArray() 
        ); 
      $form->setData($data); 
      if (! $form->isValid()) { 
       return ['form' => $form];  
      } 
      else{   
        $import->exchangeArray($form->getData()); 
        $data = $form->getData(); 
        $fileName = $data['DCL_Path']['name']; 

$form->setInputFilter($import->getInputFilter());正确的位置?或者我何时必须绑定Inputfilter才能形成?

有一个小问题,左起:我现在有一个消息:

文件类型不匹配

我试着上传。XLSX文件

回答

1

请尝试一下本作InputFilter

 $inputFilter->add([ 
      'type'  => 'Zend\InputFilter\FileInput', 
      'name'  => 'DCL_Path', // Element's name. 
      'required' => true, // Whether the field is required. 
      'filters' => [  // Filters. 
       [ 
        'name' => \Zend\Filter\File\RenameUpload::class, 
        'options' => [ 
         'use_upload_extension' => true, 
         'randomize' => false, 
         'overwrite' => true, 
         'target' => 'public/files', 
        ], 
       ], 
      ], 
      'validators' => [  // Validators. 
       [ 
        'name' => \Zend\Validator\File\Extension::class, 
        'options' => [ 
         'extension' => 'xls, xlsx', 
         'message' => 'File extension not match', 
        ], 
       ], 
       [ 
        'name' => \Zend\Validator\File\MimeType::class, 
        'options' => [ 
         'mimeType' => 'text/xls', 'text/xlsx', 
         'message' => 'File type not match', 
        ], 
       ], 
       [ 
        'name' => \Zend\Validator\File\Size::class, 
        'options' => [ 
         'max' => '4MB', 
         'message' => 'File too large', 
        ], 
       ], 
      ] 
     ]); 

这里的控制器

if($this->getRequest()->isPost()) { 
     // merge post and files 
     $request = $this->getRequest(); 
     $data = array_merge_recursive(
      $request->getPost()->toArray(), 
      $request->getFiles()->toArray() 
     ); 

     // passing data 
     $form->setData($data); 

     // execute validator 
     if($form->isValid()) { 
      // execute file filters. 
      $data = $form->getData(); 
     } 
    } 

通过使用\Zend\Validator\File\Extension\Zend\Validator\File\MimeType\Zend\Validator\File\FileSize你不需要使用此代码手动检查你的位指示。

if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) {} 

因为验证将在我们呼叫$form->isValid()时执行。使用\Zend\Filter\File\RenameUpload,您不需要再使用move_uploaded_file()。因为此过滤器会将上传的文件移到我们在'target' => 'public/files'选项中定义的目标文件。当我们调用$form->getData();

而关于解释ValidatorFilter,我建议你创建另一个职位是执行

过滤。通过使用单独的问题,在搜索引擎中搜索将很容易,并且可以帮助其他人找到它。

+0

第一:您的帖子真的很有帮助,我明白当执行验证时以及过滤器工作正常时,我的建议是正确的,在重复之前。第二我得到一个错误,这可能会很快修复:在插件管理器Zend \ Validator \ ValidatorPluginManager中找不到名为“Zend \ Validator \ File \ FileSize”的插件我是否必须通过作曲者来获取它? –

+0

好吧,应该是\ Zend \ Validator \ File \ FilesSize但现在我有一个额外的问题,我选择了一个文件,提交后我得到这个验证消息:“值是必需的,不能为空” –

+0

对不起, \ Zend \ Validator \ File \ Size'而不是'Zend \ Validator \ File \ FileSize' –