Files & Zend_Form

Starting with Zend Framework 1.8, the ‘File’ component of Zend Form (Zend_Form_Element_File) has had significant updates making it much more intuitive and easy to use. The most noteworthy changes are:

  1. The Zend_Form_Element_File object now includes an HTTP transfer object
  2. The form->getValues() method now automatically uploads the file (i.e., it makes the receive() call)

Here is a simple usage scenario describing the salient features:

1. File.php – extends the zend_form and creates a file field:

<?php
class Form_File extends Zend_Form
{
    public function init()
    {
        $this->setAction(‘index’);
        $this->setAttrib(‘id’, ‘form1’);
        $this->setAttrib(‘enctype’, ‘multipart/form-data’);

        $file1 = $this->createElement(‘File’, ‘file1’);
        $file1->setLabel(‘Upload file1’);
        $file1->setDestination(APPLICATION_PATH.’/uploads’);
        $file1->addValidator(new Zend_Validate_File_FilesSize(array(‘min’=>1,
            ‘max’=>10000,’bytestring’=>true)));
        $file1->addValidator(new Zend_Validate_File_Extension(array(‘txt,pdf,docx’)));
        $this->addElement($file1);

        $submit = $this->createElement(‘submit’, ‘Submit’);
        $this->addElement($submit);
    }
}
?>

The ‘enctype’ needs to be set to multipart/form-data. The destination of the file is set to the ‘uploads’ directory using the ‘setDestination()’ function. Next, basic input validations are applied to the file. Zend_Validate_File_FilesSize specifies min and max values for bytesize. Zend_Validate_File_Extension allows only files of certain extension (txt, pdf, docx in our case)

2. The index action (within IndexController)

public function indexAction()
    {
        // action body
        $form = new Form_File();
        $request = $this->getRequest();
        $this->view->form = $form;
        if ($request->isPost())
        {
            //normal submit..
            if ($form->isValid($_POST))
            {
                //determine filename and extension
                $info = pathinfo($form->file1->getFileName(null,false));
                $filename = $info[‘filename’];
                $ext = $info[‘extension’]?”.”.$info[‘extension’]:””;
                //filter for renaming.. prepend with current time
                $form->file1->addFilter(new Zend_Filter_File_Rename(array(
                                “target”=>time().$filename.$ext,
                                “overwrite”=>true)));
                $form->getValue(‘file1’);
            }
        }
    }

The process above demonstrates how you can use the Zend_Filter_File_Rename filter to make changes to your file name prior to saving (note that the filter is added to file1 BEFORE the getValue() call).

  • The getFileName(null,false) call retrieves the file name WITHOUT the full path.
  • PHP function pathinfo() is used to determine the filename and extension
  • The file extension is retrieved without the period(.).. so, a basic check for extension information allows us to prepend the ‘.’ (note that this is strictly not required for the example above because we use the Zend_Validate_File_Extension to enforce extension types)
  • The ‘Rename’ filter is attached to the file1 control. In this case, we simply prepend the current Unix time stamp to the file name
  • After the getValue() call, the selected file is moved to the ‘/uploads’ directory with the new file name! (of course appropriate file permissions must be granted to the directory)

3. Index.phtml view script (in application/views/scripts/index)

<?php
echo $this->form;
?>

Advertisements

One thought on “Files & Zend_Form

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s