How to download files in Zend Framework 2?

I am viewing file upload in ZF2.

I understand that many of you will think this is too vague a question, but what is the best way to create form elements that are slightly processed?

I canโ€™t figure out where to start. I ruled out processing it in the controller, as this would violate the DRY principles. The form object does not seem to have a place to โ€œplug inโ€ any code. The view helper is just for presentation, so there is no point in doing anything. Thus, this leaves an input filter. This also seems wrong.

I was sent to transfer adapters, but the code does not look very ZF2.

I am sorry that this is such a vague question, and I hope that he will fall into pretty ears. He is diligently studying a structure that has very little documentation and is compounded by the fact that my knowledge of zend framework 1 is a little subtle, progress a bit slow.

As soon as I have a good work example, I may find a place to publish.

+6
source share
5 answers

it's simple:
[In your controller]

$request = $this->getRequest(); if($request->isPost()) { $files = $request->getFiles()->toArray(); $httpadapter = new \Zend\File\Transfer\Adapter\Http(); $filesize = new \Zend\Validator\File\Size(array('min' => 1000 )); //1KB $extension = new \Zend\Validator\File\Extension(array('extension' => array('txt'))); $httpadapter->setValidators(array($filesize, $extension), $files['file']['name']); if($httpadapter->isValid()) { $httpadapter->setDestination('uploads/'); if($httpadapter->receive($files['file']['name'])) { $newfile = $httpadapter->getFileName(); } } } 

UPDATE : I found a better way to use file validation using form validation:
Add this class to your module, for example: Application / Validators / File / Image.php

 <?php namespace Application\Validators\File; use Application\Validator\FileValidatorInterface; use Zend\Validator\File\Extension; use Zend\File\Transfer\Adapter\Http; use Zend\Validator\File\FilesSize; use Zend\Filter\File\Rename; use Zend\Validator\File\MimeType; use Zend\Validator\AbstractValidator; class Image extends AbstractValidator { const FILE_EXTENSION_ERROR = 'invalidFileExtention'; const FILE_NAME_ERROR = 'invalidFileName'; const FILE_INVALID = 'invalidFile'; const FALSE_EXTENSION = 'fileExtensionFalse'; const NOT_FOUND = 'fileExtensionNotFound'; const TOO_BIG = 'fileFilesSizeTooBig'; const TOO_SMALL = 'fileFilesSizeTooSmall'; const NOT_READABLE = 'fileFilesSizeNotReadable'; public $minSize = 64; //KB public $maxSize = 1024; //KB public $overwrite = true; public $newFileName = null; public $uploadPath = './data/'; public $extensions = array('jpg', 'png', 'gif', 'jpeg'); public $mimeTypes = array( 'image/gif', 'image/jpg', 'image/png', ); protected $messageTemplates = array( self::FILE_EXTENSION_ERROR => "File extension is not correct", self::FILE_NAME_ERROR => "File name is not correct", self::FILE_INVALID => "File is not valid", self::FALSE_EXTENSION => "File has an incorrect extension", self::NOT_FOUND => "File is not readable or does not exist", self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected", self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected", self::NOT_READABLE => "One or more files can not be read", ); protected $fileAdapter; protected $validators; protected $filters; public function __construct($options) { $this->fileAdapter = new Http(); parent::__construct($options); } public function isValid($fileInput) { $options = $this->getOptions(); $extensions = $this->extensions; $minSize = $this->minSize; $maxSize = $this->maxSize; $newFileName = $this->newFileName; $uploadPath = $this->uploadPath; $overwrite = $this->overwrite; if (array_key_exists('extensions', $options)) { $extensions = $options['extensions']; } if (array_key_exists('minSize', $options)) { $minSize = $options['minSize']; } if (array_key_exists('maxSize', $options)) { $maxSize = $options['maxSize']; } if (array_key_exists('newFileName', $options)) { $newFileName = $options['newFileName']; } if (array_key_exists('uploadPath', $options)) { $uploadPath = $options['uploadPath']; } if (array_key_exists('overwrite', $options)) { $overwrite = $options['overwrite']; } $fileName = $fileInput['name']; $fileSizeOptions = null; if ($minSize) { $fileSizeOptions['min'] = $minSize*1024 ; } if ($maxSize) { $fileSizeOptions['max'] = $maxSize*1024 ; } if ($fileSizeOptions) { $this->validators[] = new FilesSize($fileSizeOptions); } $this->validators[] = new Extension(array('extension' => $extensions)); if (! preg_match('/^[a-z0-9-_]+[a-z0-9-_\.]+$/i', $fileName)) { $this->error(self::FILE_NAME_ERROR); return false; } $extension = pathinfo($fileName, PATHINFO_EXTENSION); if (! in_array($extension, $extensions)) { $this->error(self::FILE_EXTENSION_ERROR); return false; } if ($newFileName) { $destination = $newFileName.".$extension"; if (! preg_match('/^[a-z0-9-_]+[a-z0-9-_\.]+$/i', $destination)) { $this->error(self::FILE_NAME_ERROR); return false; } } else { $destination = $fileName; } $renameOptions['target'] = $uploadPath.$destination; $renameOptions['overwrite'] = $overwrite; $this->filters[] = new Rename($renameOptions); $this->fileAdapter->setFilters($this->filters); $this->fileAdapter->setValidators($this->validators); if ($this->fileAdapter->isValid()) { $this->fileAdapter->receive(); return true; } else { $messages = $this->fileAdapter->getMessages(); if ($messages) { $this->setMessages($messages); foreach ($messages as $key => $value) { $this->error($key); } } else { $this->error(self::FILE_INVALID); } return false; } } } 

Use in form and add filterInput:

  array( 'name' => 'file', 'required' => true, 'validators' => array( array( 'name' => '\Application\Validators\File\Image', 'options' => array( 'minSize' => '64', 'maxSize' => '1024', 'newFileName' => 'newFileName2', 'uploadPath' => './data/' ) ) ) ) 

And in your controller:

  $postData = array_merge_recursive((array)$request->getPost(), (array)$request->getFiles()); $sampleForm->setData($postData); if ($sampleForm->isValid()) { //File will be upload, when isValid returns true; } else { var_dump($sampleForm->getMessages()); } 
+7
source

This is an old question, but in case someone else got here, I found this nice article:

Create a simple file verification upload form

This is a good start to working with files in ZF2.;)

+5
source

From what I asked / saw in irc (# Zftalk.2), the file component has not yet been reorganized

+2
source

to get $ _FILES, you do the following in the controller:

 $request= $this->getRequest(); if($request->isPost()){ $post= array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray()); print_r($post); } 

This post is old, but I hope this helps someone.

0
source

Downloading the factory file and checking scheduled for ZF2.1

While I'm using $ _FILES :(

Check out the following links:

-1
source

Source: https://habr.com/ru/post/921654/


All Articles