A free and open-source book on Zend Framework for beginners

10.2. Accessing Uploaded Files in ZF3

In your controller class, you typically do not communicate with the $_FILES array directly, instead you may use the Request class or the Params controller plugin, as shown in code example below:

class IndexController extends AbstractActionController 
    // An example controller action intended for handling file uploads.
    public function uploadAction() 
        // Get the whole $_FILES array.
        $files = $this->getRequest()->getFiles();
        // The same, but with Params controller plugin.
        $files = $this->params()->fromFiles();
        // Get a single entry of the $_FILES array.
        $files = $this->params()->fromFiles('myfile');

In line 9 of the code above, we use the getRequest() method of the controller class for accessing the Request object, and the getFiles() method of the request object to retrieve the information about all upload files at once.

In line 12, we do the same thing with the Params controller plugin. We use its fromFiles() method to get the information about all uploaded files.

If needed, you can extract the information for the specific file only. In line 15, we use the same fromFiles() method and pass it the name of the file field to retrieve. This retrieves the single file entry from the $_FILES super-global array.