Return to Snippet

Revision: 37046
at December 3, 2010 03:01 by nikefido


Initial Code
<?php

class App_Model_Upload {
	
	private $upload;
	
	public function __construct($path = '/home/account/public_html/uploads') {				
		$this->upload = new Zend_File_Transfer_Adapter_Http();
		$this->upload->addValidator('Extension', false, 'jpg,jpeg,png,gif')
					 ->addValidator('Size', false, 2097152) //max of 2mb = 2097152
				     ->addValidator('Count', false, array('min' => 1, 'max' => 2));
			        /* ->addValidator('ImageSize', false, array('minwidth' => 100,
														'maxwidth' => 100,
														'minheight' => 1000,
														'maxheight' => 1000))*/
				
			   
		$renameFilter = new Zend_Filter_File_Rename( $path );

		$files = $this->upload->getFileInfo();
		foreach($files as $fileID => $fileInfo) {
			if(!$fileInfo['name']=='') {
				$renameFilter->addFile(array(
					'source' => $fileInfo['tmp_name'], 
					'target' => $path.'/'.time().'_'.$fileInfo['name'],  //Set timeindex on file name for uniqueness
					'overwrite' => true ) 
				);
			}
		} 
		// add filters to Zend_File_Transfer_Adapter_Http
		$this->upload->addFilter($renameFilter);
		
		
		return $this;
	}
	
	public function isValid() {
		return $this->upload->isValid();
	}
	
	public function getMessages() {
		return $this->upload->getMessages();
	}
	
	public function upload() {
		try {
			$this->upload->receive();
		} 
		catch (Zend_File_Transfer_Exception $e) {
			//This is a tad dirty
			throw new Exception('Bad file data: '.$e->getMessage());
		}
		return $this;
	}
	
	public function getUpload() {
		return $this->upload;
	}
}

Initial URL

                                

Initial Description
A sample wrapper class around Zend_File_Transfer_Adapter_Http

Initial Title
Zend file uploading class

Initial Tags
file

Initial Language
PHP