zend-frameworkfile-uploadzend-file

Zend File multiple upload with access to seperate file names


I use Zend Framework 1.12 for some file upload system. And using Zend_File_Transfer_Adapter_Http in a form, for upload of two files. There are two form elements for those two files.

$file1 = new Zend_Form_Element_File('file1');
// other options like setLabel etc.
$this->addElement($file1, 'file1');

$file2 = new Zend_Form_Element_File('file2');
// other options like setLabel etc.
$this->addElement($file2, 'file2');

and I handle the upload process in my controller like this:

if ($request->isPost()) {
                    if ($form->isValid($request->getPost())) {

                        $adapter = new Zend_File_Transfer_Adapter_Http();
                        $adapter->setDestination($dirname);
                        $files = $adapter->getFileInfo();

                        foreach ($files as $file => $fileInfo) {


                            if (!$adapter->receive($file)) {
                                $messages = $adapter->getMessages();
                                echo implode("\n", $messages);
                            } else {
                                $location = $adapter->getFileName($file, true);
                                $filename = $adapter->getFileName($file, false);


                               // taking location and file names to save in database..
                            }
                        }
                    }

With these, I can manage upload of two files. But I don't know how to take location of files which is uploaded with the specific Zend_Form_Element_File. For example, i need to know which file is uploaded to $file1 (element in form) and I will save its location to a table in database, and which file is uploaded to $file2 and save its location to another table.


Solution

  • I do not like to use Zend_File_Transfer_Adapter_Http. I prefer to use code like this:

    in application.ini:

    data_tmp = APPLICATION_PATH "/../data/tmp"
    

    in Bootstrap:

    $options = $this->getOptions();
    define('DATA_TMP', $options['data_tmp']);
    

    in form:

    $elementFoo = new Zend_Form_Element_File('foo');
    $elementFoo->setLabel('Upload File 1:')->setDestination(DATA_TMP);
    
    $elementBar = new Zend_Form_Element_File('bar');
    $elementBar->setLabel('Upload File 2:')->setDestination(DATA_TMP);
    

    in controller:

    if ($request->isPost()) {
        if ($form->isValid($request->getPost())) {
           $values = $form->getValues();
           $filenameFoo = $values['foo'];
           $filenameBar = $values['bar'];
           //at this point you know the name of the individual filename
           $filePathFoo = DATA_TMP . DIRECTORY_SEPARATOR . $filenameFoo;
           $filePathBar = DATA_TMP . DIRECTORY_SEPARATOR . $filenameBar;
           //now you have even the physical path of the files
    
           // taking location and file names to save in database..
        }
    }
    

    my 2 cent.