Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Form_Element_File to rename file saving extension

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;
                }       
like image 447
Oleg Avatar asked Mar 08 '11 11:03

Oleg


3 Answers

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.

like image 126
Marcin Avatar answered Nov 02 '22 02:11

Marcin


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.

like image 33
ACNB Avatar answered Nov 02 '22 00:11

ACNB


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();
like image 20
Ashley Avatar answered Nov 02 '22 01:11

Ashley