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.

    To upload files to the server using forms, the Zend Framework Form has the Zend_Form_Element_File element. It has a Rename filter, which allows you to rename the downloaded file. But there is a minus - we can’t just specify a new name for the file so that its extension is preserved. 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 valid extensions

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

    So, we have the form, now it will accept 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 () - take the information about the uploaded file image

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

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

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

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

    Also Zend_Form_Element_File has a setMultiFile () method, which makes it possible to send several fa . catch element form one 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 that will be downloaded using the “Count” validator

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


    But there is one big “BUT”. The Rename filter will rename all files to one. How to be in this situation? There is an exit. We go again 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 the File_Transfer_Adapter directly and its receive () method. But then forget about the “Count” validator, because it will read the wrong number of files. Also, errors are displayed for only 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 some elements of the File form and everything will work fine.

    Also popular now: