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
- <?php
- /*
- * PHP File Upload Class
- *
- * Class to deal with easily uploading files.
- * It ensures that the upload file:
- * - Is not too large
- * - Is of an allowed file type
- * - Will not overwrite an existing file with the same name
- *
- * Written By Jacob Wyke – jacob@redvodkajelly.com – www.redvodkajelly.com
- *
- * LICENSE
- * ——-
- * 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.
- *
- * USAGE
- * —–
- *
- * Simply setup the class and then call either showForm() or processUpload()
- *
- * $objUpload = new RVJ_FileUpload(‘uploads’);
- *
- * To display a default upload form:
- *
- * echo $objUpload->showForm();
- *
- * To process an upload
- *
- * echo $objUpload->processUpload();
- *
- */
- class RVJ_FileUpload {
- var $arrAllowedTypes = array();
- var $strUploadName;
- var $numFileMaxSize;
- var $strFileName;
- var $strUploadDirectory;
- var $strPreText;
- var $strSaveLocation;
-
- /*
- *
- * @Method: __constructor
- * @Parameters: 5
- * @Param-1: strDirectory – String – The path to the upload directory
- * @Param-2: numFileMaxSize – Number – The maxium file upload size in Bytes
- * @Param-3: arrAllowedTypes – Array – An array of allowed file types
- * @Param-4: strFieldName – String – The name of the upload field
- * @Param-5: strPreText – String – Any text that you want to prepend to the uploaded file name
- * @Description: Constructor method for PHP 5
- *
- */
- function __constructor($strDirectory = ‘uploads’, $numFileMaxSize = 512000, $arrAllowedTypes = 0, $strFieldName = ‘upload’, $strPreText = ”){
- $this->RVJ_FileUpload($strDirectory, $numFileMaxSize, $arrAllowedTypes, $strFieldName, $strPreText);
- }
-
- /*
- *
- * @Method: RVJ_FileUpload
- * @Parameters: 5
- * @Param-1: strDirectory – String – The path to the upload directory
- * @Param-2: numFileMaxSize – Number – The maxium file upload size in Bytes
- * @Param-3: arrAllowedTypes – Array – An array of allowed file types
- * @Param-4: strFieldName – String – The name of the upload field
- * @Param-5: strPreText – String – Any text that you want to prepend to the uploaded file name
- * @Description: Finds files within a directory
- *
- */
- function RVJ_FileUpload($strDirectory = ‘uploads’, $numFileMaxSize = 512000, $arrAllowedTypes = 0, $strFieldName = ‘upload’, $strPreText = ”){
- if(!$arrAllowedTypes){
- //use the default allowed type list
- $this->arrAllowedTypes = $this->getFileTypes();
- }else{
- //use custom set allowed list
- $this->arrAllowedTypes = $arrAllowedTypes;
- }
-
- //save the parameters passed
- $this->strUploadName = $strFieldName;
- $this->numFileMaxSize = $numFileMaxSize;
- $this->strUploadDirectory = $strDirectory;
- $this->strPreText = $strPreText;
- }
-
- /*
- *
- * @Method: getFileTypes
- * @Parameters: 0
- * @Description: Returns an array of allowed file types
- *
- */
- function getFileTypes(){
- return array(
- ’text/html’,
- ’text/plain’,
- ’text/css’,
- ’image/gif’,
- ’image/x-png’,
- ’image/jpeg’,
- ’image/tiff’,
- ’image/png’,
- ’image/x-ms-bmp’,
- ’audio/x-wav’,
- ’application/x-pn-realaudio’,
- ’video/mpeg’,
- ’video/quicktime’,
- ’video/x-msvideo’,
- ’application/postscript’,
- ’application/rtf’,
- ’application/pdf’,
- ’application/x-pdf’,
- ’application/x-gtar’,
- ’application/x-tar’,
- ’application/zip’,
- ’application/x-zip-compressed’,
- ’application/mac-binhex40′,
- ’application/x-stuffit’,
- ’application/octet-stream’,
- ’text/javascript’,
- ’application/x-javascript’,
- ’application/x-sh’,
- ’application/x-csh’,
- ’application/x-perl’,
- ’application/x-tcl’,
- ’application/vnd.ms-powerpoint’,
- ’application/ms-powerpoint’,
- ’application/vnd.ms-excel’,
- ’application/msword’,
- ’video/avi’,
- ’java/*’,
- ’application/java’,
- ’image/x-icon’,
- ’image/bmp’,
- ’image/pjpeg’,
- ’application/x-bittorrent’
- );
- }
-
- /*
- *
- * @Method: showForm
- * @Parameters: 1
- * @Param-1: strFormAction – String – The form action
- * @Description: Returns the HTML to show an upload form
- *
- */
- function showForm($strFormAction = ”){
- return "
- <form method=\"post\" action=\"{$strFormAction}\" name=\"file_upload\" enctype=\"multipart/form-data\">
- File: <input type=\"file\" name=\"{$this->strUploadName}\" size=\"25\">
- <br />
- <input type=\"submit\" name=\"submit\" value=\"Upload\">
- </form>
- ";
- }
-
- /*
- *
- * @Method: processUpload
- * @Parameters: 0
- * @Description: Processes the file upload
- *
- */
- function processUpload(){
- //ensure that the upload file exists
- if(!file_exists($_FILES[$this->strUploadName]['tmp_name'])){
- return "There was an error uploading the file.";
- }else{
- //set the location of the file
- $this->strFileName = $_FILES[$this->strUploadName]['name'];
-
- //check to make sure the file isnt too big
- if($_FILES[$this->strUploadName]['size']>$this->numFileMaxSize){
- return "The uploaded file was too large";
- }else{
- //check to make sure the file is of the allowed types
- if(!in_array($_FILES[$this->strUploadName]['type'], $this->arrAllowedTypes)){
- return "This type of file is not allowed to be uploaded.";
- }else{
- $this->strSaveLocation = $this->strUploadDirectory."/".$this->strPreText.$this->strFileName;
- $numLoop = 1;
-
- //ensure that the file name is unique – or else add a version number to the end of it to make it unique
- while(file_exists($this->strSaveLocation)){
- $numPos = strpos($this->strFileName, ‘.’);
- $this->strSaveLocation = $this->strUploadDirectory."/".$this->strPreText.substr($this->strFileName,0,$numPos)."-".$numLoop.substr($this->strFileName,$numPos);
- $numLoop++;
- }
- if(!move_uploaded_file($_FILES[$this->strUploadName]['tmp_name'], $this->strSaveLocation)){
- return "There was an error saving the file.";
- }else{
- return "File Successfully Uploaded.";
- }
- }
- }
- }
- }
-
- }
- ?>


