RedVodkaJelly Logo

PHP File Upload Class

POSTED AT 15:35 on 23rd May 2008

Simple PHP class to upload files and check there type and size.

It offers a simple upload form and a processing method that ensures that the file is:
Of an allowed file type.
Not larger than a stated size.
Not named the same as another file in the directory (and so prevents over witting).

Source Code

  1. <?php
     
  2. /*
     
  3. * PHP File Upload Class
     
  4. *
     
  5. * Class to deal with easily uploading files.
     
  6. * It ensures that the upload file:
     
  7. *   - Is not too large
     
  8. *   - Is of an allowed file type

     

  9. *   - Will not overwrite an existing file with the same name
     
  10. *
     
  11. * Written By Jacob Wyke – jacob@redvodkajelly.com – www.redvodkajelly.com
     
  12. *
     
  13. * LICENSE
     
  14. * ——-
     
  15. * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used.
     
  16. *
     
  17. * USAGE
     
  18. * —–
     
  19. *
     
  20. * Simply setup the class and then call either showForm() or processUpload()
     
  21. *
     
  22. *   $objUpload = new RVJ_FileUpload(‘uploads’);
     
  23. *

     

  24. * To display a default upload form:
     
  25. *
     
  26. *   echo $objUpload->showForm();
     
  27. *
     
  28. * To process an upload
     
  29. *
     
  30. *   echo $objUpload->processUpload();
     
  31. *
     
  32. */
     
  33.  
  34. class RVJ_FileUpload {
     
  35.  
  36.    var $arrAllowedTypes = array();   

     

  37.    var $strUploadName;
     
  38.    var $numFileMaxSize;
     
  39.    var $strFileName;
     
  40.    var $strUploadDirectory;
     
  41.    var $strPreText;
     
  42.    var $strSaveLocation;
     
  43.    
     
  44.    /*
     
  45.    *
     
  46.    *   @Method:      __constructor
     
  47.    *   @Parameters:   5
     
  48.    *   @Param-1:      strDirectory – String – The path to the upload directory

     

  49.    *   @Param-2:      numFileMaxSize – Number – The maxium file upload size in Bytes
     
  50.    *   @Param-3:      arrAllowedTypes – Array – An array of allowed file types
     
  51.    *   @Param-4:      strFieldName – String – The name of the upload field
     
  52.    *   @Param-5:      strPreText – String – Any text that you want to prepend to the uploaded file name
     
  53.    *   @Description:   Constructor method for PHP 5
     
  54.    *
     
  55.    */   

     

  56.    function __constructor($strDirectory = ‘uploads’, $numFileMaxSize = 512000, $arrAllowedTypes = 0, $strFieldName = ‘upload’, $strPreText = ”){
     
  57.       $this->RVJ_FileUpload($strDirectory, $numFileMaxSize, $arrAllowedTypes, $strFieldName, $strPreText);
     
  58.    }
     
  59.    
     
  60.    /*
     
  61.    *
     
  62.    *   @Method:      RVJ_FileUpload
     
  63.    *   @Parameters:   5
     
  64.    *   @Param-1:      strDirectory – String – The path to the upload directory
     
  65.    *   @Param-2:      numFileMaxSize – Number – The maxium file upload size in Bytes

     

  66.    *   @Param-3:      arrAllowedTypes – Array – An array of allowed file types
     
  67.    *   @Param-4:      strFieldName – String – The name of the upload field
     
  68.    *   @Param-5:      strPreText – String – Any text that you want to prepend to the uploaded file name
     
  69.    *   @Description:   Finds files within a directory
     
  70.    *
     
  71.    */   
     
  72.    function RVJ_FileUpload($strDirectory = ‘uploads’, $numFileMaxSize = 512000, $arrAllowedTypes = 0, $strFieldName = ‘upload’, $strPreText = ”){
     
  73.       if(!$arrAllowedTypes){                                       

     

  74.          //use the default allowed type list
     
  75.          $this->arrAllowedTypes = $this->getFileTypes();
     
  76.       }else{
     
  77.          //use custom set allowed list
     
  78.          $this->arrAllowedTypes = $arrAllowedTypes;   
     
  79.       }
     
  80.       
     
  81.       //save the parameters passed
     
  82.       $this->strUploadName = $strFieldName;
     
  83.       $this->numFileMaxSize = $numFileMaxSize;
     
  84.       $this->strUploadDirectory = $strDirectory;

     

  85.       $this->strPreText = $strPreText;
     
  86.    }
     
  87.    
     
  88.    /*
     
  89.    *
     
  90.    *   @Method:      getFileTypes
     
  91.    *   @Parameters:   0
     
  92.    *   @Description:   Returns an array of allowed file types
     
  93.    *
     
  94.    */

     

  95.    function getFileTypes(){
     
  96.       return array(
     
  97.          ’text/html’,
     
  98.          ’text/plain’,
     
  99.          ’text/css’,
     
  100.          ’image/gif’,
     
  101.          ’image/x-png’,
     
  102.          ’image/jpeg’,
     
  103.          ’image/tiff’,
     
  104.          ’image/png’,
     
  105.          ’image/x-ms-bmp’,
     
  106.          ’audio/x-wav’,
     
  107.          ’application/x-pn-realaudio’,
     
  108.          ’video/mpeg’,
     
  109.          ’video/quicktime’,
     
  110.          ’video/x-msvideo’,
     
  111.          ’application/postscript’,

     

  112.          ’application/rtf’,
     
  113.          ’application/pdf’,
     
  114.          ’application/x-pdf’,
     
  115.          ’application/x-gtar’,
     
  116.          ’application/x-tar’,
     
  117.          ’application/zip’,
     
  118.          ’application/x-zip-compressed’,
     
  119.          ’application/mac-binhex40′,
     
  120.          ’application/x-stuffit’,
     
  121.          ’application/octet-stream’,
     
  122.          ’text/javascript’,
     
  123.          ’application/x-javascript’,
     
  124.          ’application/x-sh’,
     
  125.          ’application/x-csh’,
     
  126.          ’application/x-perl’,
     
  127.          ’application/x-tcl’,
     
  128.          ’application/vnd.ms-powerpoint’,

     

  129.          ’application/ms-powerpoint’,
     
  130.          ’application/vnd.ms-excel’,
     
  131.          ’application/msword’,
     
  132.          ’video/avi’,
     
  133.          ’java/*’,
     
  134.          ’application/java’,
     
  135.          ’image/x-icon’,
     
  136.          ’image/bmp’,
     
  137.          ’image/pjpeg’,
     
  138.          ’application/x-bittorrent’
     
  139.       );
     
  140.    }
     
  141.    
     
  142.    /*
     
  143.    *
     
  144.    *   @Method:      showForm

     

  145.    *   @Parameters:   1
     
  146.    *   @Param-1:      strFormAction – String – The form action
     
  147.    *   @Description:   Returns the HTML to show an upload form
     
  148.    *
     
  149.    */   
     
  150.    function showForm($strFormAction = ”){
     
  151.       return "
     
  152.          <form method=\"post\" action=\"{$strFormAction}\" name=\"file_upload\" enctype=\"multipart/form-data\">

     

  153.             File: <input type=\"file\" name=\"{$this->strUploadName}\" size=\"25\">
     
  154.             <br />
     
  155.             <input type=\"submit\" name=\"submit\" value=\"Upload\">

     

  156.          </form>
     
  157.       ";
     
  158.    }
     
  159.    
     
  160.    /*
     
  161.    *
     
  162.    *   @Method:      processUpload
     
  163.    *   @Parameters:   0
     
  164.    *   @Description:   Processes the file upload
     
  165.    *

     

  166.    */   
     
  167.    function processUpload(){
     
  168.       //ensure that the upload file exists
     
  169.       if(!file_exists($_FILES[$this->strUploadName]['tmp_name'])){
     
  170.          return "There was an error uploading the file.";
     
  171.       }else{
     
  172.          //set the location of the file
     
  173.          $this->strFileName = $_FILES[$this->strUploadName]['name'];
     
  174.          
     
  175.          //check to make sure the file isnt too big
     
  176.          if($_FILES[$this->strUploadName]['size']>$this->numFileMaxSize){

     

  177.             return "The uploaded file was too large";
     
  178.          }else{
     
  179.             //check to make sure the file is of the allowed types            
     
  180.             if(!in_array($_FILES[$this->strUploadName]['type'], $this->arrAllowedTypes)){
     
  181.                return "This type of file is not allowed to be uploaded.";
     
  182.             }else{
     
  183.                $this->strSaveLocation = $this->strUploadDirectory."/".$this->strPreText.$this->strFileName;

     

  184.                $numLoop = 1;
     
  185.                
     
  186.                //ensure that the file name is unique – or else add a version number to the end of it to make it unique
     
  187.                while(file_exists($this->strSaveLocation)){
     
  188.                   $numPos = strpos($this->strFileName, ‘.’);
     
  189.                   $this->strSaveLocation = $this->strUploadDirectory."/".$this->strPreText.substr($this->strFileName,0,$numPos)."-".$numLoop.substr($this->strFileName,$numPos);   

     

  190.                   $numLoop++;
     
  191.                }
     
  192.  
  193.                if(!move_uploaded_file($_FILES[$this->strUploadName]['tmp_name'], $this->strSaveLocation)){
     
  194.                   return "There was an error saving the file.";
     
  195.                }else{
     
  196.                   return "File Successfully Uploaded.";
     
  197.                }
     
  198.             }
     
  199.          }
     
  200.       }

     

  201.    }
     
  202.    
     
  203. }
     
  204.  
  205. ?>