Coder Social home page Coder Social logo

php-file-upload-class's Introduction

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)

php-file-upload-class's People

Contributors

aivis avatar andrewprofile avatar sigsalerno avatar shankka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.