📜 ⬆️ ⬇️

Download and rename files

The article is written for those who are already at least a little familiar with the architecture of the Zend Framework. If anyone is interested, I will describe working with forms in more detail in a separate article.

For uploading files to the server using forms in the Zend Framework Form there is an element Zend_Form_Element_File. It has a filter "Rename", which allows you to rename the downloaded file. But there is a minus - we can not just specify a new name for the file so that its extension will be saved. How to do it? And what if we use setMultiFile?


')
For example, we have the following form

class Form_Myform extends Zend_Form
{
public function __construct($options = null )
{
parent::__construct($options);

$ this ->setAttrib( 'enctype' , 'multipart/form-data' );

$image = new Zend_Form_Element_File( 'image' );
$image->setLabel( 'Image:' )
->addValidator( 'Size' , false , 1024000)
->addValidator( 'Extension' , false , 'jpg,png,gif' );

$submit = new Zend_Form_Element_Submit( 'go' );
$submit->setLabel( 'Submit' );

$elements = array($image, $submit);
$ this ->addElements($elements);
}


* This source code was highlighted with Source Code Highlighter .


addValidator ('Size', false, 1024000) - set the maximum size (1000kB)

addValidator ('Extension', false, 'jpg, png, gif') - specify valid extensions

We do not specifically specify setDestination, because we will use the Rename filter.

So, we have the form, will now take the file. We climb into the controller and write the following

$request = $ this ->getRequest();
$form = new Form_Myform();
if ($request->isPost()) {
if ( $form->isValid( $request->getPost() ) ) {
$file = $form->image->getFileInfo();
$ext = split( "[/\\.]" , $file[ 'image' ][ 'name' ]);
$newName = 'newname.' .$ext[count($ext)-1];

$form->image->addFilter( 'Rename' , realpath(dirname( '.' )).
DIRECTORY_SEPARATOR.
'upload' .
DIRECTORY_SEPARATOR.
$newName);

$form->image->receive();
}
}


* This source code was highlighted with Source Code Highlighter .


$ file = $ form-> image-> getFileInfo () - we take information about the uploaded file image

$ ext = split ("[/ \\.]", $ file ['image'] ['name']) - cut the extension from the file name

$ newName = 'newname.'. $ ext - we set a new name with the old extension (the name can be generated, if desired, randomly)

$ form-> image-> addFilter ('Rename' ... - add the “Rename” filter to the image form element, where we pass the new name + full path to the file on the server

$ form-> image-> receive () - transfer the file to our folder from temporary. The filter is applied automatically.

Zend_Form_Element_File also has a setMultiFile () method, which allows you to send multiple files in one form element at once. For example:

$image = new Zend_Form_Element_File( 'image' );
$image->setLabel( 'Image:' )
->addValidator( 'Size' , false , 1024000)
->addValidator( 'Extension' , false , 'jpg,png,gif' )
->setMultiFile(3);


* This source code was highlighted with Source Code Highlighter .


In this case, all filters and validators will apply to all files at once. You can even specify the minimum and maximum number of files to be loaded using the “Count” validator.

->addValidator( 'Count' , false , array( 'min' => 1, 'max' => 3))


But there is one big "BUT". The filter "Rename" renames all files "in one". How to be in this situation? There is an exit. Go back to the controller:

$request = $ this ->getRequest();
$form = new Form_Myform();
if ($request->isPost()) {
if ( $form->isValid( $request->getPost() ) ) {
$adapter = $form->image->getTransferAdapter();
$i = 0;
foreach ($adapter->getFileInfo() as $file) {
$ext = split( "[/\\.]" , $file[ 'name' ]);
$newName = 'newname' .$i. '.' .$ext[count($ext)-1];
$adapter->addFilter( 'Rename' , realpath(dirname( '.' )).
DIRECTORY_SEPARATOR.
'upload' .
DIRECTORY_SEPARATOR.
$newName);
$adapter->receive($file[ 'name' ]);
$i++;
}
}
}


* This source code was highlighted with Source Code Highlighter .


In this case, we use File_Transfer_Adapter directly and its receive () method. But then forget about the validator "Count", because It will count the wrong number of files. Also, errors are displayed only for one file, even if they were all.

I advise you, if you can do without setMultiFile (), then it is better not to use it. Better create several File form elements and everything will work fine.

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


All Articles