Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.input-filter.file-input.html

Size:16790
Storage flags:no_autoload,compress/gzip (25%)

File Upload Input — Zend Framework 2 2.4.2 documentation

File Upload Input

The Zend\InputFilter\FileInput class is a special Input type for uploaded files found in the $_FILES array.

While FileInput uses the same interface as Input, it differs in a few ways:

  1. It expects the raw value to be in the $_FILES array format.
  2. The validators are run before the filters (which is the opposite behavior of Input). This is so that any is_uploaded_file() validation can be run prior to any filters that may rename/move/modify the file.
  3. Instead of adding a NotEmpty validator, it will (by default) automatically add a Zend\Validator\File\UploadFile validator.

The biggest thing to be concerned about is that if you are using a <input type="file"> element in your form, you will need to use the FileInput instead of Input or else you will encounter issues.

Basic Usage

Usage of FileInput is essentially the same as Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use Zend\Http\PhpEnvironment\Request;
use Zend\Filter;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Input;
use Zend\InputFilter\FileInput;
use Zend\Validator;

// Description text input
$description = new Input('description'); // Standard Input type
$description->getFilterChain()           // Filters are run first w/ Input
            ->attach(new Filter\StringTrim());
$description->getValidatorChain()        // Validators are run second w/ Input
            ->attach(new Validator\StringLength(array('max' => 140)));

// File upload input
$file = new FileInput('file');           // Special File Input type
$file->getValidatorChain()               // Validators are run first w/ FileInput
     ->attach(new Validator\File\UploadFile());
$file->getFilterChain()                  // Filters are run second w/ FileInput
     ->attach(new Filter\File\RenameUpload(array(
         'target'    => './data/tmpuploads/file',
         'randomize' => true,
     )));

// Merge $_POST and $_FILES data together
$request  = new Request();
$postData = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());

$inputFilter = new InputFilter();
$inputFilter->add($description)
            ->add($file)
            ->setData($postData);

if ($inputFilter->isValid()) {           // FileInput validators are run, but not the filters...
    echo "The form is valid\n";
    $data = $inputFilter->getValues();   // This is when the FileInput filters are run.
} else {
    echo "The form is not valid\n";
    foreach ($inputFilter->getInvalidInput() as $error) {
        print_r ($error->getMessages());
    }
}

Also see

Table Of Contents

Previous topic

Input filter specifications

Next topic

Introduction

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to File Upload Input on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net