Thursday 10 October 2013

Uploader class for cakephp

In this Post Iam writing code for uploading files in cakephp....

Create Uploader.php in Model folder & paste below code :-
<?php
 class Uploader extends AppModel
    {
        private $destinationPath;
        private $errorMessage;
        private $extensions;
        private $allowAll;
        private $maxSize;
        private $uploadName;
private $seqnence;
public $name='Uploader';
public $useTable =false;
        
        function setDir($path){
            $this->destinationPath  =   $path;
            $this->allowAll =   false;
        }
        
        function allowAllFormats(){
            $this->allowAll =   true;
        }
        
        function setMaxSize($sizeMB){
            $this->maxSize  =   $sizeMB * (1024*1024);
        }
        
        function setExtensions($options){
            $this->extensions   =   $options;
        }
        
function setSameFileName(){
$this->sameFileName = true;
$this->sameName = true;
}
        function getExtension($string){
            $ext = "";
            try{
                    $parts = explode(".",$string);
                    $ext = strtolower($parts[count($parts)-1]);
            }catch(Exception $c){
                    $ext = "";
            }
            return $ext;
}
        
        function setMessage($message){
            $this->errorMessage =   $message;
        }
        
        function getMessage(){
            return $this->errorMessage;
        }
        
        function getUploadName(){
            return $this->uploadName;
        }
        function setSequence($seq){
$this->imageSeq = $seq;
}

function getRandom(){
return strtotime(date('Y-m-d H:i:s')).rand(1111,9999).rand(11,99).rand(111,999);
}
function sameName($true){
$this->sameName = $true;
}
        function uploadFile($fileBrowse){ 
            $result =   false;
$size   =  $fileBrowse["size"];
            $name   =  $fileBrowse["name"];
            //$size   =   $_FILES[$fileBrowse]["size"];
            //$name   =   $_FILES[$fileBrowse]["name"];
            $ext    =   $this->getExtension($name);
            if(!is_dir($this->destinationPath)){
                $this->setMessage("Destination folder is not a directory ");
            }else if(!is_writable($this->destinationPath)){
                $this->setMessage("Destination is not writable !");
            }else if(empty($name)){
                $this->setMessage("File not selected ");
            }else if($size>$this->maxSize){
                $this->setMessage("Too large file !");
            }else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
                
if($this->sameName==false){
                $this->uploadName   =  $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
                }else{
$this->uploadName= $name;
}
                if(move_uploaded_file($fileBrowse["tmp_name"],$this->destinationPath.$this->uploadName)){
                    $result =   true;
                }else{
                    $this->setMessage("Upload failed , try later !");
                }
            }else{
                $this->setMessage("Invalid file format !");
            }
            return $result;
        }
        
        function deleteUploaded(){
            unlink($this->destinationPath.$this->uploadName);
        }
        
    }  ?>

Now I will explain how to use it..

In the controller file include Uploder model in the $uses array.

the...
if the View file code is like this:-

<form name="fileform" method="post" action="<?php echo $urlRoot; ?>image/add"/>
<input type="file" name="txtFile"/>

</form>
Now  create folder media and then add index to core.php
Configure::write('MediaDir',dirname(dirname(__FILE__)).'/media/');

Now in controller folder,code like this

if(!empty($_FILES['txtFile']['name'])){ 
$mediaBaseDir = Configure::read('MediaDir');//Media Base Directory//created outside root folder,path set in the core.php
if(!is_dir($mediaBaseDir.'news')){
mkdir($mediaBaseDir.'news');
chmod($mediaBaseDir.'news',0777);
}
$mediaPath = $mediaBaseDir.'news'.'/';
$this->Uploader->setDir($mediaPath);
$this->Uploader->setExtensions(array('jpg','jpeg','png','gif'));
$this->Uploader->setMaxSize(.8);
$this->Uploader->setSequence('news'); 
$filename = $_FILES['txtFile'];
if($this->Uploader->uploadFile($filename)){ //upload success
$image = $this->Uploader->getUploadName();

}
}



No comments:

Post a Comment