Zend Framework: Upload File Demo

Info.Tech, PHP, Web Development, Zend Framework Add comments

I’ve came across in uploading file using Zend Framework (ZF)… Since ZF is new to me, its like I’m studying PHP just like when I started, I have to memorize the predefined variables and functions but in ZF, its their classes, defined variables and its functions. Here’s my codes though its not perfect but its working.


class IndexController extends Zend_Controller_Action

    public function init()
        /* Initialize action controller here */

    public function indexAction()
        // action body
        $form = new Application_Form_Upload();

		private function getFileExtension($filename)
			$fext_tmp = explode('.',$filename);
			return $fext_tmp[(count($fext_tmp) - 1)];

		public function uploadAction()
			$dest_dir = "uploads/";

			/* Uploading Document File on Server */
			$upload = new Zend_File_Transfer_Adapter_Http();
						 ->addValidator('Count', false, 1)
						 ->addValidator('Size', false, 1048576)
						 ->addValidator('Extension', false, 'jpg,png,gif,pdf');
			$files = $upload->getFileInfo();

			// debug mode [start]
			echo '<hr />
			echo '	</pre>
						<hr />';
			// debug mode [end]

				// upload received file(s)
			catch (Zend_File_Transfer_Exception $e)

			$mime_type = $upload->getMimeType('doc_path');
			$fname = $upload->getFileName('doc_path');
			$size = $upload->getFileSize('doc_path');
			$file_ext = $this->getFileExtension($fname);
			$new_file = $dest_dir.md5(mktime()).'.'.$file_ext;

			$filterFileRename = new Zend_Filter_File_Rename(
					'target' => $new_file, 'overwrite' => true


			if (file_exists($new_file))
				$request = $this->getRequest();
				$caption = $request->getParam('caption');

				$html = 'Orig Filename: '.$fname.'<br />';
				$html .= 'New Filename: '.$new_file.'<br />';
				$html .= 'File Size: '.$size.'<br />';
				$html .= 'Mime Type: '.$mime_type.'<br />';
				$html .= 'Caption: '.$caption.'<br />';
				$html = 'Unable to upload the file!';



Below is the source file of whole project in Zend Framework for you to test it out… Enjoy!

Source Code: ZF Upload File Demo (1483)

  • Ruwan Chathuranga

    Hi, I tried your code in my project. But it didn’t work. So i created another zend application and put your code there, It also didn’t work. Can you please help me on this?

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in