Zend_Form_Element_File to rename file saving extension
Asked Answered
T

3

5

I use Zend Form and upload file. I need to rename and user addFilter for it. But if I try to get extension of the file as in the code I get an error "Too much files, maximum '1' are allowed but '2' are given". If I try to get extension using $_FILES it looks like it can work out but it seems ugly. Could you please tell me how to rename file saving it's extension?

        $form = new Form_ImportSubscribers();               
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                //it looks like it works but it's ugly solution
//              $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); 
                //causes an error "Too much files, maximum '1' are allowed but '2' are given"           
                $extension = pathinfo($form->file->getFileName(), PATHINFO_EXTENSION);              
                $form->file->addFilter('Rename', $accountId . '_' . time() .  '.' . $extension);

                if (!$form->file->receive()) {
                    $this->view->form = $form;
                    $this->view->listName = $list->list_name;                                   
                    return;
                }       
Thoer answered 8/3, 2011 at 11:35 Comment(3)
can you tell which line of code threw that error?Carping
If I use this line, then validator fails to validate when I try to upload a file.Thoer
I found similar a link with similar problems framework.zend.com/issues/browse/….Thoer
L
14

I assume that you upload multiple files if $form->file->getFileName() returns more than one value. In this case you could apply Rename filter and receive individual files as follows:

/*@var $adapter Zend_File_Transfer_Adapter_Http */
$adapter =  $form->file->getTransferAdapter();
$receivingOK = true; 
foreach ($adapter->getFileInfo() as $file => $info) {                                     
    $extension = pathinfo($info['name'], PATHINFO_EXTENSION); 
    $adapter->addFilter('Rename', $accountId . '_' . time() .  '.' . $extension, $file);
    if (!$adapter->receive($file)) {
         $receivingOK = false;
    }
}

if (!$receivingOK) {
    $this->view->form = $form;
    $this->view->listName = $list->list_name;                                   
    return;
} 

It should also work even if you don't perform multiple files upload.

Lydalyddite answered 8/3, 2011 at 13:27 Comment(4)
I don't use mutliple uploads, thet is why I find strange this error.Thoer
$form->file->getFileName() returns only one file, but zend form validation fails with error: "Too much files, maximum '1' are allowed but '2' are given"Thoer
It looks like this validator $element->addValidator('Count', false, 1); of the form fails, but it strange because I don't get multiple files.Thoer
Thank you very much. It look like this code works. It seems strange to me that my code doesn't work, I think Zend_Form should allow not to use Zend_File_Transfer_Adapter_Http directly.Thoer
A
2

This is my code in a controller, it works in Zend 1.11:

$uploadForm = new Application_Form_DeckblattUpload();
$front = $uploadForm->getElement('front');
/* @var $front Zend_Form_Element_File */
$tfa = $front->getTransferAdapter();
/* @var $tfa Zend_File_Transfer_Adapter_Abstract */
$tfa->addFilter('Rename', array(
    'target' => APPLICATION_PATH .'/public/dir/somepath.jpg',
    'overwrite' => true));

$back = $uploadForm->getElement('back');
/* @var $front Zend_Form_Element_File */
$tfa = $back->getTransferAdapter();
/* @var $tfa Zend_File_Transfer_Adapter_Abstract */
$tfa->addFilter('Rename', array(
    'target' => APPLICATION_PATH . '/public/someOtherDir/somepath.jpg',
     'overwrite' => true));

if ($this->getRequest()->upload) {
    if ($uploadForm->isValid($this->getRequest()->getParams())) {
        $uploadForm->getValues(); //<- this does the uploading
        //Success-Message
    }
    else {
        //Failure-Message
    }
}
$this->view->uploadForm = $uploadForm;

The form contains just 2 Zend_Form_Elements_Files ('back' and 'front) and the submit button ('upload').

I hope this gives you an idea how to user the rename filter and still use $form->isValid(). Don't confuse http://framework.zend.com/manual/en/zend.file.transfer.filters.html and http://framework.zend.com/manual/en/zend.filter.input.html

Also keep in mind, that you cannot use the 'viewHelper' in decorators for the file elements. Instead use 'File'. Hope this still helps.

Adulterous answered 30/9, 2011 at 11:54 Comment(0)
B
1

Try using something along the lines of:

$upload = new Zend_File_Transfer();
// use setDestination, addValidator etc
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
    $file = '/path/to/file/name.ext';
    $upload->addFilter('Rename', $file);
    //Do rest of code
}
$upload->receive();
Bowes answered 8/3, 2011 at 12:17 Comment(1)
Pardon, I don't quite understand now how to integrate it with Zend_Form.Thoer

© 2022 - 2024 — McMap. All rights reserved.