Download and rename files
the Article is written for those who are already at least somewhat familiar with the architecture of the Zend Framework. If anyone is interested — I will describe the working with forms in more detail in a separate article.
To download the files to the server with forms in Zend Framework Form there is an element Zend_Form_Element_File. It does have a filter "Rename", which allows you to rename zakochany file. But we can't just specify a new name for the file to save its extension. How to do it? But what if we use setMultiFile?
For example, we have the following form
addValidator('Size', false, 1024000) — set the maximum size (1000kB)
addValidator('Extension', false, 'jpg,png,gif') — specify the allowed extensions
We specifically do not specify setDestination because we use the Rename filter.
So, the form we have now will accept the file. Climb into the controller and write the following
$file = $form->image->getFileInfo() — take information about the uploaded image file
$ext = split("[/\\.]", $file['image']['name']) — cut out the extension from a file name
$newName = 'newname.'.$ext — set a new name with the old extension (name can be generated, if desired, randomly)
$form->image->addFilter('Rename' ... — added filter "Rename" item on the form image, where to say the new name + the full path to the file on the server
$form->image->receive() — transfer the file to our folder from the temporary. The filter is applied automatically.
Also from Zend_Form_Element_File is a method setMultiFile(), which allows you to send multiple files in a single form element. For example:
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 downloaded using the validator "Count"
But there is one big "BUT". Filter "Rename" rename all files "in one". How to be in this situation? There is a solution. Go back to the controller:
In this case we use directly File_Transfer_Adapter and its method receive(). But then forget about the validator "Count" because he would take the wrong number of files. The same error appear only for one file, even if they were at all.
Suggest, if you can do without the setMultiFile(), it is better not use. Create several items of the form File and everything will work fine.
Article based on information from habrahabr.ru
To download the files to the server with forms in Zend Framework Form there is an element Zend_Form_Element_File. It does have a filter "Rename", which allows you to rename zakochany file. But we can't just specify a new name for the file to save its extension. How to do it? But 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 the allowed extensions
We specifically do not specify setDestination because we use the Rename filter.
So, the form we have now will accept the file. 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() — take information about the uploaded image file
$ext = split("[/\\.]", $file['image']['name']) — cut out the extension from a file name
$newName = 'newname.'.$ext — set a new name with the old extension (name can be generated, if desired, randomly)
$form->image->addFilter('Rename' ... — added filter "Rename" item on the form image, where to say the new name + the full path to the file on the server
$form->image->receive() — transfer the file to our folder from the temporary. The filter is applied automatically.
Also from Zend_Form_Element_File is a method setMultiFile(), which allows you to send multiple files in a single form element. 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 downloaded using the validator "Count"
->addValidator('Count' false, array('min' => 1, 'max' => 3))
But there is one big "BUT". Filter "Rename" rename all files "in one". How to be in this situation? There is a solution. 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 directly File_Transfer_Adapter and its method receive(). But then forget about the validator "Count" because he would take the wrong number of files. The same error appear only for one file, even if they were at all.
Suggest, if you can do without the setMultiFile(), it is better not use. Create several items of the form File and everything will work fine.
Комментарии
Отправить комментарий