OpenSencillo  2015.002
upload Class Reference

Public Member Functions

 __construct ($path)
 mimeConfig ($mime=null)
 path ($path)
 maxSize ($size)
 advancedMode ($mode=null)
 ajaxSendJson ($respond=null)
 name ()
 upload ()

Protected Member Functions

 mimeTest ($mime)

Protected Attributes

 $mime
 $uploadDirectory
 $size
 $uploadInfo
 $mode
 $status

Detailed Description

Definition at line 2 of file files.upload.fup.php.


Constructor & Destructor Documentation

__construct ( path)

Definition at line 11 of file files.upload.fup.php.

        {
                $this->mime=null;
                $this->path($path);
                $this->advancedMode();
        }

Member Function Documentation

advancedMode ( mode = null)

Advanced upload mode TRUE/FALSE, deafult FALSE

Parameters:
bool$mode

Definition at line 84 of file files.upload.fup.php.

        {
                switch($mode)
                {
                        case true:
                                $this->mode = true;
                        break;
                        default:
                                $this->mode = false;
                        break;
                }
        }
ajaxSendJson ( respond = null) [final]

Json status encoder for AJAX comunication

Definition at line 100 of file files.upload.fup.php.

        {
                if($respond==null)
                {
                        print json_encode($this->status);
                }
                else
                {
                        print json_encode($this->status[$respond]);
                }
        }
maxSize ( size)

Maximum allowed file size

Parameters:
int$size

Definition at line 74 of file files.upload.fup.php.

        {
                $this->size = $size;
        }
mimeConfig ( mime = null)

Config method for legal mime types

Parameters:
string$mime

default, if $mime is null

Definition at line 36 of file files.upload.fup.php.

        {
                if(is_array($mime))
                {
                        foreach($mime as $val)
                        {
                                $this->mimeTest($val);
                        }
                }
                else 
                {
                        $this->mimeTest($mime);
                }
                
                if($mime==null)
                {
                        $this->mime=true;
                }
        }
mimeTest ( mime) [protected]

Internal protected function for detection legal mime type

Parameters:
mixed$mime

Definition at line 23 of file files.upload.fup.php.

        {
                if($val == $_FILES['FileInput']['type'])
                {
                        $this->mime=true;
                }
        }
name ( )

Get new file name

Definition at line 115 of file files.upload.fup.php.

        {
                return $this->uploadInfo['newName'];
        }
path ( path)

specify upload directory ends with / (slash)

Parameters:
string$path

Definition at line 64 of file files.upload.fup.php.

        {
                $this->uploadDirectory = $path;//example '/home/website/file_upload/uploads/'
        }
upload ( )

Upload method

200 - OK 404 - Not found / Unknown error / No file 413 - Request is to large (maximum size is low) 415-0 - Unsupported Media Type (acquired in upload method) 415-1 - Unsupported Media Type (acquired in mimeConfig method) 417 - Expectation Failed (failed moving file from temporary location) 444 - No Response / Internal AJAX fail

Definition at line 131 of file files.upload.fup.php.

        {
                $this->status = array();
                if(isset($_FILES["FileInput"])/* && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK*/)
                {
                        $UploadDirectory = $this->uploadDirectory; //specify upload directory ends with / (slash)
                         
                        /*
                         Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
                         Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
                         and set them adequately, also check "post_max_size".
                         */
                         
                        //check if this is an ajax request
                        if($this->mode)
                        {
                                $this->status['mode']='advanced';
                                if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
                                {
                                        $this->status['code']="444";
                                }
                                 
                                //Is file size is less than allowed size.
                                if ($_FILES["FileInput"]["size"] > $this->size) 
                                {
                                        $this->status['code']="413";
                                }
                                 
                                //allowed file type Server side check
                                if($this->mime==true)
                                {
                                        switch(strtolower($_FILES['FileInput']['type']))
                                        {
                                                //allowed file types
                                                case 'image/png':
                                                case 'image/gif':
                                                case 'image/jpeg':
                                                case 'image/pjpeg':
                                                case 'text/plain':
                                                case 'text/html': //html file
                                                case 'application/x-zip-compressed':
                                                case 'application/pdf':
                                                case 'application/msword':
                                                case 'application/vnd.ms-excel':
                                                case 'video/mp4':
                                                        $this->status['mime']=$_FILES['FileInput']['type'];
                                                        break;
                                                default:
                                                        $this->status['code']="415-0"; //output error
                                        }
                                }
                                else 
                                {
                                        $this->status['code']="415-1";//output error
                                }
                        }
                        else
                        {
                                $this->status['mode']='simple';
                        }
                         
                        $File_Name          = strtolower($_FILES['FileInput']['name']);
                        $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
                        $Random_Number      = rand(0, 9999999999).date('YmdHis'); //Random number to be added to name.
                        $NewFileName        = $Random_Number.$File_Ext; //new file name
                        
                        $this->uploadInfo = array('oldName'=>$File_Name,
                                                                          'ext'=>$File_Ext,
                                                                          'rnd'=>$Random_Number,
                                                                          'newName'=>$NewFileName);
                        $this->status['info'] = $this->uploadInfo;
                        
                        if(!is_dir($UploadDirectory))
                        {
                            mkdir($UploadDirectory,0777);
                        }
                        if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName))
                        {
                                // do other stuff
                                $this->status['code']="200";
                                $this->status['name']=$NewFileName;
                        }
                        else
                        {
                                $this->status['code']="417";
                        }
                        
                }
                else
                {
                        $this->status['code']="404";
                }
                
                if($this->status['code']=='200')
                {
                    $this->ajaxSendJson('name');
                }
                else
                {
                    $this->ajaxSendJson('code');
                }
        }

Field Documentation

$mime [protected]

Definition at line 4 of file files.upload.fup.php.

$mode [protected]

Definition at line 8 of file files.upload.fup.php.

$size [protected]

Definition at line 6 of file files.upload.fup.php.

$status [protected]

Definition at line 9 of file files.upload.fup.php.

$uploadDirectory [protected]

Definition at line 5 of file files.upload.fup.php.

$uploadInfo [protected]

Definition at line 7 of file files.upload.fup.php.


The documentation for this class was generated from the following file:
 All Data Structures Files Functions Variables