Skip to content
Simple file validation & upload class
PHP
Branch: master
Clone or download

Latest commit

aivis Merge pull request #23 from andrewprofile/master
Fixed typo in property name
Latest commit 26ae43b Apr 23, 2018

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md wiki moved to README.md Jun 22, 2013
test.php added: test.php file Dec 3, 2011
upload.php Fixed typo in property name Apr 23, 2018

README.md

Simple php upload class with file validation.

You need to install (or enable in php.ini) PHP "file info" extension.

Link: http://us2.php.net/manual/en/fileinfo.installation.php

Quick start

<?php
require_once 'upload.php';

if (!empty($_FILES['test'])) {
	
	$upload = Upload::factory('important/files');
	$upload->file($_FILES['test']);
	
	$results = $upload->upload();
	
	var_dump($results);
	
}

Simple validation

<?php

require_once 'upload.php';

if (!empty($_FILES['test'])) {
	
	$upload = Upload::factory('important/files');
	$upload->file($_FILES['test']);
	
	//set max. file size (in mb)
	$upload->set_max_file_size(1);
	
	//set allowed mime types
	$upload->set_allowed_mime_types(array('application/pdf'));
	
	$results = $upload->upload();
	
	var_dump($results);
}

Callbacks

<?php
require_once 'upload.php';

class validation {
	
	public function check_name_length($object) {
		
		if (mb_strlen($object->file['original_filename']) > 5) {
			
			$object->set_error('File name is too long.');
			
		}

	}
	
}


if (!empty($_FILES['test'])) {
	
	$upload = Upload::factory('important/files');
	$upload->file($_FILES['test']);
	
	$validation = new validation;
	
	$upload->callbacks($validation, array('check_name_length'));
	
	$results = $upload->upload();
	
	var_dump($results);
	
}

$result dump

array
  'status' => boolean false
  'destination' => string 'important/files/' (length=16)
  'size_in_bytes' => int 466028
  'size_in_mb' => float 0.44
  'mime' => string 'application/pdf' (length=15)
  'original_filename' => string 'About Stacks.pdf' (length=16)
  'tmp_name' => string '/private/var/tmp/phpXF2V7o' (length=26)
  'post_data' => 
    array
      'name' => string 'About Stacks.pdf' (length=16)
      'type' => string 'application/pdf' (length=15)
      'tmp_name' => string '/private/var/tmp/phpXF2V7o' (length=26)
      'error' => int 0
      'size' => int 466028
  'errors' => 
    array
      0 => string 'File name is too long.' (length=22)

$upload->upload();

is equivalent

if ($upload->check()) {		
	$upload->save();	
}
$upload->get_state();

Use this to get validation errors.

$upload->get_errors();

When upload done you also get new filename and full path

'filename' => string '091755cc57ee634186cd2655c3a0ec990c36f9161322940586' (length=50)
'full_path' => string 'important/files/091755cc57ee634186cd2655c3a0ec990c36f9161322940586' (length=66)
You can’t perform that action at this time.