File Uploading

File uploading elements for a form are also available. With the Zend_Form_Element_File class, you can transform a simple XHTML form into a file uploading tool that allows users to upload anything from MP3 files to images onto your server.

The Zend_Form_Element_File class contains additional setters and validators you can use to control such things as the destination of the file as well as the types of files a user is allowed to upload (see Table 4-10).

Table 4-10. Zend_Form_Element_File Methods

Function Description setDestination() Sets the path to the destination where the files will be saved.

setMultiFile() Sets the total number of files to upload.

setMaxFileSize() Sets the maximum file size.

isUploaded() Determines whether the file has been uploaded.

isReceived() Determines whether the file has been received by the server.

isFiltered() Determines whether any filtering has been done to the file.

addValidator() Adds constraints to the file upload. Acceptable values: count, size, extension.

Using the methods in Table 4-10, let's update the account update form to allow users the ability to add an avatar to their profile page. First you need a place to store the images. Create a folder called users in your public directory. This folder will be the final destination for any user-uploaded avatar. The folder will contain only one profile image per user, and each image will be renamed to the user's unique ID as soon as the file has been uploaded.

Listing4-39. Updated getUpdateForm()

* Update Form

private function getUpdateForm() {

//Create Form


$form->setAttrib('sitename', 'loudbite');

$form->setAttrib('enctype', 'multipart/form-data');

//Load Elements class require "Form/Elements.php"; $LoudbiteElements = new Elements();

//Create Username Field.

$form->addElement($LoudbiteElements->getUsernameTextField()); //Create Email Field.

$form->addElement($LoudbiteElements->getEmailTextField()); //Create Password Field.

$form->addElement($LoudbiteElements->getPasswordTextField()); //Create Text Area for About me.

$textAreaElement = new Zend_Form_Element_TextArea('aboutme'); $textAreaElement->setLabel('About Me:'); $textAreaElement->setAttribs(array('cols' => 15,

'rows' => 5)); $form->addElement($textAreaElement);

//Add File Upload

$fileUploadElement = new Zend_Form_Element_File('avatar'); $fileUploadElement->setLabel('Your Avatar:'); $fileUploadElement->setDestination('../public/users'); $fileUploadElement->addValidator('Count', false, 1); $fileUploadElement->addValidator('Extension', false, 'jpg,gif); $form->addElement($fileUploadElement);

//Create a submit button. $form->addElement('submit', 'submit'); $submitElement = $form->getElement('submit'); $submitElement->setLabel('Update My Account');

return $form;

The bold code shown in Listing 4-39 contains the updated getUpdateForm() method, which contains the code to add an additional element to the form and sets the form enctype attribute. You use the Zend_Form_Element_File class to create a file upload input field. You set the label, the destination, a validator indicating the maximum number of files to upload, and a validator to restrict the user in uploading any file not of type jpg or gif. Finally, add the element to the form. With the form created, you need to update the updateAction() method. As it is now, the updateAction() method will not save the file. Listing 4-40 contains the updated code.

Listing 4-40. Updating Form with File Upload

* Update the User's data.

public function updateAction() {

//Check if the user is logged in //Fetch the user's id //Fetch the users information

//Create the form.

//Check if the form has been submitted. //If so validate and process. if($_POST){

//Check if the form is valid. if($form->isValid($_POST)){

//Get the values

$username = $form->getValue('username'); $password = $form->getValue('password'); $email = $form->getValue('email'); $aboutMe = $form->getValue('aboutme');

//Otherwise redisplay the form. else{

//Otherwise display the form. else{

Listing 4-40 contains a single update to the method. The bold line moves the uploaded file from the PHP tmp directory to the destination folder specified in the setDestination() method. Now load the URL http://localhost/account/update and upload an avatar. As a test, open the destination folder public/user to verify that the file you uploaded is present.

Uploading multiple files is done much the same way. If you require the user to upload multiple files, use the Zend_Form_Element_File::setMultiFile() method, passing in the correct number of files a user can upload.

Was this article helpful?

0 0

Post a comment