phpvalidationzend-frameworkzend-framework3

Inputfilter for fileupload, basic usage


Now I'm a bit more into this ZF3 stuff. I could (with some help) implement nearly everything I wanted. To dive in the, for me new version, I developed a test project.

Some questions are still unanswered and I didn't find usable explanations.

My new issue is InputFilterAwareInterface. I tried the examples for strings from the tutorial, so far everything ok. But like always for the easy topics you find everything, if you go further it ends abruptly.

I need an Inputfilter for xls and xlsx files. I googled of course, read tutorials, searched in the zend tutorial, because I had the idea there must somewhere exist some complete reference, but I couldn't find any.

So I tried this one:

    $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
//                              ]
//                      ]
//              ],
        ]); 

As you an see I'm still fighting the validator part. What would be the right syntax to validate xls and xlsx files with a maximum size of let's say 4 MB?

And after that, what about the filterarea, I did the following in my controller action just because I'm used to

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!';
                    }

Is the move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName); obsolet with the filterstuff in the interface? And again how would be the syntax in this case?

And one of my biggest wish, does somebody know kind of a tutorial which explains plainly the different possibilities and keys of both options (validator and filter)? Sometimes I can't believe that you need so much time to find only the right keys.

EDIT 1: Show Form, filterstuff and changed controller

here is part my Form class:

<?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.
//              ]
        ]);

here part of the class extended 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',
                                ],
                        ],
                ]
        ]);

and my part of my controlleraction, I think inhere might be the problem, something is probably not logic:

$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: Post part of controlleraction to discuss order of some statements:

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'];

Is the position of $form->setInputFilter($import->getInputFilter()); correct? Or when do I have to bind the Inputfilter to form?

There is a small issue left: I now have a message:

File type not match

I tried to upload a .xlsx file


Solution

  • Please try this for 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',
                        ],
                    ],
                ]
            ]);
    

    And here for controller

        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();
            }
        }
    

    By using \Zend\Validator\File\Extension, \Zend\Validator\File\MimeType and \Zend\Validator\File\FileSize you don't need to check manually in your contoller using this code.

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

    Because validation will be executed when we call $form->isValid().

    And by using \Zend\Filter\File\RenameUpload, you don't need to use move_uploaded_file() anymore. Because this filter will move the uploaded file to destination foder we defined in 'target' => 'public/files' option.

    Filtering is executed when we call $form->getData();

    And about explanation for Validator and Filter, I suggest you to create another post. By using a separate question, it will be easy to search in search engine and will help another to find it.