Zend file uploading class


/ Published in: PHP
Save to your folder(s)

A sample wrapper class around Zend_File_Transfer_Adapter_Http


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class App_Model_Upload {
  4.  
  5. private $upload;
  6.  
  7. public function __construct($path = '/home/account/public_html/uploads') {
  8. $this->upload = new Zend_File_Transfer_Adapter_Http();
  9. $this->upload->addValidator('Extension', false, 'jpg,jpeg,png,gif')
  10. ->addValidator('Size', false, 2097152) //max of 2mb = 2097152
  11. ->addValidator('Count', false, array('min' => 1, 'max' => 2));
  12. /* ->addValidator('ImageSize', false, array('minwidth' => 100,
  13. 'maxwidth' => 100,
  14. 'minheight' => 1000,
  15. 'maxheight' => 1000))*/
  16.  
  17.  
  18. $renameFilter = new Zend_Filter_File_Rename( $path );
  19.  
  20. $files = $this->upload->getFileInfo();
  21. foreach($files as $fileID => $fileInfo) {
  22. if(!$fileInfo['name']=='') {
  23. $renameFilter->addFile(array(
  24. 'source' => $fileInfo['tmp_name'],
  25. 'target' => $path.'/'.time().'_'.$fileInfo['name'], //Set timeindex on file name for uniqueness
  26. 'overwrite' => true )
  27. );
  28. }
  29. }
  30. // add filters to Zend_File_Transfer_Adapter_Http
  31. $this->upload->addFilter($renameFilter);
  32.  
  33.  
  34. return $this;
  35. }
  36.  
  37. public function isValid() {
  38. return $this->upload->isValid();
  39. }
  40.  
  41. public function getMessages() {
  42. return $this->upload->getMessages();
  43. }
  44.  
  45. public function upload() {
  46. try {
  47. $this->upload->receive();
  48. }
  49. catch (Zend_File_Transfer_Exception $e) {
  50. //This is a tad dirty
  51. throw new Exception('Bad file data: '.$e->getMessage());
  52. }
  53. return $this;
  54. }
  55.  
  56. public function getUpload() {
  57. return $this->upload;
  58. }
  59. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.