FileUploader

For the purpose of this documentation we shall be using the following sample data

TEST DATA
 
    $_FILES['name']  = 'Foo'; 
    $_FILES['type']  = 'image/png'; 
    $_FILES['size']  = 5000000; //5mb
    $_FILES['tmp_name']   = '/tmp/files/image.png'; 
    $_FILES['error'] = ''; 

                                

initializing class
The file uploader class can be easily initialized as shown below.

Sample: Initializing Input
    $FileUploader  = new FileUploader;
                                

start
The start method is used to set parameters into the input class.

Syntax: start
    $FileUploader->start($files, $type);
    
      where:
            
       $files: $_FILES or files data array
       $type: When set as 'image', it allows internal validation of images selected.
    
                                    
Example: setting files
    $FileUploader->start($_FILES); // set files for upload

    $FileUploader->start($_FILES, 'image'); // set image files for upload
                                

filesize
The filesize() method sets the maximum number of bytes the files uploaded must not exceed

Syntax: filesize
    $FileUploader->filesize($size); 
        
      where:
            
        $size: maximum file size in bytes
        
                                        
Example: filesize
    $FileUploader->start($_FILES);

    $FileUploader->filesize(2000000);  // 2mb                            
                                        

GetFileName
This method returns the file name of the currently set file.

Syntax: GetFileName
    $FileUploader->GetFileName();  // returns name of file
                                        
Example: GetFileName
    $FileUploader->start($_FILES, 'image');
    $FileUploader->GetFileName();  // returns Foo
                                        

GetFileType
This method returns the type of current file set.

Syntax: GetFileType
    $FileUploader->GetFileType();  // returns type of file 
                                        
Example: GetFileType
    $FileUploader->start($_FILES, 'image');
    $FileUploader->GetFileType();  // image/png
                                        

GetFileSize
This method returns the size of current file set.

Syntax: GetFileSize
    $FileUploader->GetFileSize();  // returns size of file 
                                        
Example: GetFileSize
    $FileUploader->start($_FILES, 'image');
    $FileUploader->GetFileSize();  // 5000000
                                        

GetFileTemp
This method returns the tmp_name of current file set.

Syntax: GetFileTemp
    $FileUploader->GetFileTemp();  // returns the temporary directory of file 
                                    
Example: GetFileTemp
    $FileUploader->start($_FILES, 'image');
    $FileUploader->GetFileTemp();  // /tmp/files/image.png
                                    

GetFileError
This method returns the error for current file set.

Syntax: GetFileError
  $FileUploader->GetFileError();  // returns the current file error 
                                    
Example: GetFileError
  $FileUploader->start($_FILES, 'image');
  $FileUploader->GetFileError();  // returns null
                                    

GetFileData
This method returns the data for current file set.

Syntax: GetFileData
  $FileUploader->GetFileData(bool);  //  returns string of supplied data 
    
    where: 

      bool : boolean of true adds the new directory and new file name of an uploaded file to the data string returned.
                             
Example: GetFileData
  $FileUploader->start($_FILES, 'image');
  $FileUploader->GetFileData();  //list $_FILES as string
                                    

uniqueFile
This function directs the uploader class to upload a file with a unique new name

Syntax: uniqueFile
  $FileUploader->uniqueFile($param); 
    
    where :

      $param : true permits a unique output name
                 false keeps the source file name
                 string sets a new output name. 
    
                                    
Example: uniqueFile
  $FileUploader->start($_FILES);

  $FileUploader->uniqueFile(); // generate a new output name
  $FileUploader->uniqueFile(false); // keep source name
  $FileUploader->uniqueFile('foo'); // use a unique output name foo
                                    

uploadFile
This method executes the upload directive.

Syntax: uploadFile
  $FileUploader->uploadFile($validFiles, $destination, $makedir);  // returns : true if file was uploaded 
     
    where :

       $validFiles: array list of valid or acceptable extensions.
       $destination: destination path of uploaded file.
       $makedir: bool of true creates a new directory if it does not already exist.
    
                                        
Example: uploadFile

  $FileUploader->start($_FILES, 'image');

  $FileUploader->uploadFile(['jpg','png'], dirname(__FILE__).'/images', true); 
    // 1. upload only files with jpg or png extensions
    // 2. upload into destination path supplied 
    // 3. create directory of destination if it does not exist

                                        

response
The response returns the response of the processes executed. This may be good for code debugging

Example: response
  $FileUploader->start($_FILES, 'image');

  $upload = $FileUploader->uploadFile(['jpg','png'], dirname(__FILE__).'/images', true); 

  if($upload){

      //file uploaded successfully

  } else {
        
      var_dump( $FileUploader->response() ); //return the upload response.

  }
                                        

reconstruct
The reconstruct method is used to restructure multiple files for upload

Syntax: reconstruct
  $FileUploader->reconstruct($data);
     
    where : 

      $data: array of multiple files
    
                                        
Example: reconstruct
  
  $_FILES['name'][0]  = 'Foo'; 
  $_FILES['name'][1]  = 'Bar';   

  $_FILES['type'][0]  = 'image/png'; 
  $_FILES['type'][1]  = 'image/png'; 

  $_FILES['size'][0]  = 5000000; //5mb
  $_FILES['size'][1]  = 2000000; //2mb

  $_FILES['tmp_name'][0]   = '/tmp/files/foo.png'; 
  $_FILES['tmp_name'][1]   = '/tmp/files/bar.png'; 
    
  $_FILES['error'][0] = ''; 
  $_FILES['error'][1] = ''; 
  

  $files = $FileUploader->reconstruct($_FILES);

  var_dump($files); 
    
  //The above returns: 
    
      [
        
        0 => [
            'name'     => 'Foo'; 
            'type'     => 'image/png'; 
            'size'     => 5000000; 
            'tmp_name' => '/tmp/files/foo.png';
            'error'    => ''; 
        ],
    
        1 => [
            'name'     => 'Bar'; 
            'type'     => 'image/png'; 
            'size'     => 2000000; 
            'tmp_name' => '/tmp/files/bar.png';
            'error'    => ''; 
        ],

      ] 
    
                                        
This method makes it easier to organize files for upload. Then upload can be done easily by:
...continued
  ... using previous data

  $files = $FileUploader->reconstruct($_FILES);

  foreach( $files as $file ) {
  
      $FileUploader->start($file, 'image');
      $FileUploader->uniqueFile();
  
      if($FileUploader->uploadFile(['jpg', 'png'])) {
  
          //upload successful
  
      } else {
  
          var_dump( $FileUploader->response() );
  
      }
  
  }