PHP File Upload Manager


2011-01-15 Digg! icurtain Delcious icurtain

Thought I would consolidate file upload functions for PHP into one nice easy class - compatible with dbphp - makes my life soooooo easy ;) 

  1. class FileUpload { 
  2.  
  3.    /*Documentation 
  4.    FileUpload Class 0.1 - 2010-01-15 by icurtain.co.uk 
  5.    Imeplentation is as follows 
  6.     
  7.    $file = new FileUpload(); 
  8.    $file->setUploadName('userfile'); 
  9.    $file->setDestination('x.jpg'); 
  10.    if(!$file->go()){ 
  11.       echo $file->getErrorStatus(); 
  12.    } 
  13.     
  14.    Any Comments etc: http://www.icurtain.co.uk/contact.php if you use this, please keep the header, it will make me feel all fuzzy inside
  15.    Copyright icurtain.co.uk - all rights reserved 
  16.    */ 
  17.  
  18.    private $uploadName = null; 
  19.    private $errorStatus = null; 
  20.    private $destination = null; 
  21.    //store the file here 
  22.    private $file = null; 
  23.  
  24.    public function FileUpload(){ 
  25.       //constructor - easier to put uploadName and destination in here? dunno 
  26.    } 
  27.  
  28.    public function go(){ 
  29.       if($this->checkErrorStatus()){ 
  30.          foreach($_FILES as $index => $file){ 
  31.             if($index == $this->getUploadName()){ 
  32.                //echo "found"; 
  33.                if($this->isUploaded($file)){ 
  34.                   if($this->moveFile($file)){ 
  35.                      return(true); 
  36.                   } 
  37.                }  
  38.             } 
  39.          } 
  40.       } 
  41.       //something failed 
  42.       return(false); 
  43.    } 
  44.     
  45.    private function isUploaded($file){ 
  46.       if (is_uploaded_file($file['tmp_name'])) { 
  47.          return(true); 
  48.       } 
  49.       $this->setErrorStatus("Possible file upload attack: ".$file['error']); 
  50.       return(false); 
  51.    } 
  52.     
  53.    private function moveFile($file){ 
  54.       if(!move_uploaded_file($file['tmp_name'], $this->getDestination())){ 
  55.          $this->setErrorStatus("Move failed: ".$file['error']); 
  56.          return(false); 
  57.       } 
  58.       return(true); 
  59.    } 
  60.  
  61.    private function checkErrorStatus(){ 
  62.        
  63.       if(!isset($this->uploadName)){ 
  64.          $this->setErrorStatus('no upload name'); 
  65.          return(false); 
  66.       } 
  67.        
  68.       if(!isset($this->destination)){ 
  69.          $this->setErrorStatus('no desination'); 
  70.          return(false); 
  71.       } 
  72.  
  73.       return(true); 
  74.    } 
  75.     
  76.    public function getUploadName(){ 
  77.       return $this->uploadName; 
  78.    } 
  79.  
  80.    public function setUploadName($uploadName){ 
  81.       $this->uploadName = $uploadName; 
  82.    } 
  83.  
  84.    public function getErrorStatus(){ 
  85.       return $this->errorStatus; 
  86.    } 
  87.  
  88.    private function setErrorStatus($errorStatus){ 
  89.       $this->errorStatus = $errorStatus; 
  90.    }    
  91.  
  92.     
  93.    public function getDestination(){ 
  94.       return $this->destination; 
  95.    } 
  96.  
  97.  
  98.    public function setDestination($destination){ 
  99.    //make sure the file name is jpg 
  100.       if($this->checkFileName($destination)){ 
  101.          $this->destination = $destination; 
  102.          return(true); 
  103.       } 
  104.       return(false); 
  105.    } 
  106.     
  107.    private function checkFileName($fileName){ 
  108.       if(!preg_match('/\.jpg$/i',$fileName)){ 
  109.          $this->setErrorStatus('invalid file name'); 
  110.          return(false); 
  111.       } 
  112.       return(true); 
  113.    } 
  114. }