C3rd
Zend Framework: Upload File Demo
Posted: 4 May 2012, 3:31am - FridayI'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.
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body $form = new Application_Form_Upload(); $this->view->assign('form',$form); } 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(); $upload->setDestination($dest_dir) ->addValidator('Count', false, 1) ->addValidator('Size', false, 1048576) ->addValidator('Extension', false, 'jpg,png,gif,pdf'); $files = $upload->getFileInfo(); // debug mode [start] echo '<hr /> <pre>'; print_r($files); echo ' </pre> <hr />'; // debug mode [end] try { // upload received file(s) $upload->receive(); } catch (Zend_File_Transfer_Exception $e) { $e->getMessage(); exit; } $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( array( 'target' => $new_file, 'overwrite' => true )); $filterFileRename->filter($fname); 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 />'; } else { $html = 'Unable to upload the file!'; } $this->view->assign('up_result',$html); } }Below is the source file of whole project in Zend Framework for you to test it out... Enjoy! Source Code: uploadify.zip