Scripts

Uploader class

Deze upload class gemaakt omdat ik het zat was om elke keer opnieuw upload scripts te schrijven. Deze uploader voert controles uit en load daarna de file up. In de toekomst zal ik hem nog wel verder uitbreiden met resize en watermerk en zulk soort dingetjes. Nu lekker even simpel. Ik hoop dat jullie er wat aan hebben. Commentaar is natuurlijk altijd welkom

upload_class.php
<?php

/**
 * @package uploader
 *
 * @author Niels Kieviet <[email protected]>
 * @version $Revision v1.00$
 * @copyright Copyright (c) 2010, Niels Kieviet
 */

class uploader
{
	/**
	 * @var Array
	 */
	protected $extensions;
	/**
	 * @var Array
	 */
	protected $types;
	/**
	 * @var Integer
	 */
	protected $size;
	/**
	 * @var Array
	 */
	protected $errors;
	/**
	 * @var String
	 */
	protected $hash;
	
	/**
	 * Contructor
	 * 
	 * @param Array $extensions 
	 * @param Array $types 
	 * @param Integer $size 
	 */
	public function __construct( Array $extensions = array(), Array $types = array(), $size = 5000000 )
	{
		$this->errors = array();
		$this->extensions = $extensions;
		$this->types = $types;
		$this->size = $size;
	}

	/**
	 * Upload the file
	 * 
	 * @param String $file
	 * @param String $path
	 */
	public function upload( $file, $path )
	{
		if( $this->checkFile( $file ) ) {
			if( $this->isAllowed( $file ) && $this->pathExists( $path ) ) {
				// Extension of file
				$ext = strtolower( strrchr( $file['name'], '.' ) );
				// Make a final hash
				$hash = new hash();
				$hash->createHash();
				$this->hash = $hash->getHash();
				// Upload the file to the final folder
				if( !move_uploaded_file( $file['tmp_name'], $path.$this->hash.$ext ) ) {
					$this->errors[] = 'Verplaats fout';
				}
			}
		}
		//var_dump ( $this->checkSize( $file ) );
	}
	
	/**
	 * Check if file is allow
	 * 
	 * @param String $file
	 * @return Boolean
	 */
	protected function isAllowed( $file )
	{
		$allowed = true;
		foreach( array( 'mime', 'extension', 'size' ) as $check ) {
			$method = sprintf( 'check%s', ucfirst( $check ) );
			$allowed = ( $this->$method( $file ) ) && $allowed;
		}
		return $allowed;
	}
	
	/**
	 * CheckFile
	 * 
	 * @param String $file
	 * @return Boolean
	 */
	public function checkFile( $file )
	{
		if( empty( $file['tmp_name'] ) ) {
			$this->errors[] = 'Select bestand';
			return false;
		}
		return true;
	}
	
	/**
	 * Check MimeType
	 * 
	 * @param String $file
	 * @return Boolean
	 */
	protected function checkMime( $file )
	{
		if( count( $this->types ) > 0 ) {
			if( !in_array( $file['type'], $this->types ) ) {
				$this->errors[] = 'MIME type fout';
				return false;
			}
			return true;
		}
		return true;
	}
	
	/**
	 * Check extension
	 * 
	 * @param String $file
	 * @return Boolean
	 */
	protected function checkExtension( $file )
	{
		if( count ( $this->extensions ) > 0 ) {
			// Check extension
			$ext = strtolower( strrchr( $file['name'], '.' ) );
				// Control if extension is allowed
				if( !in_array( $ext, $this->extensions ) ) {
					$this->errors[] = 'Extensie fout';
					return false;
				}
			return true;
		}
		return true;
	}
	
	/**
	 * Check size
	 * 
	 * @param String $file
	 * @return Boolean
	 */
	protected function checkSize( $file )
	{
		if( ctype_digit( $this->size ) ) {
			// Controle file size
			if( $file['size'] > $this->size ) {
				$this->errors[] = 'Grote bestand';
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Check path
	 * 
	 * @param String $path
	 * @return Boolean
	 */
	protected function pathExists( $path )
	{
		if( !empty( $path ) ) {
			if( !is_dir( $path ) ) {
				$this->errors[] = 'Upload map';
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Give the hash
	 *
	 * @return String
	 */
	public function hash( ) 
	{
		return $this->hash;
	}
	
	/**
	 * Give all errors
	 * 
	 * @return Array
	 */
	public function errors( $css )
	{
		if( sizeof( $this->errors ) > 0 ) {
			$html = '<ul class="' .$css. '">';
				// Foreach array to string
				foreach( $this->errors as $error ) {
					$html .= '<li> ' .$error. ' </li>';
				}
			$html .= '</ul>';
			return $html;
		}
	}
}
?>

use.php
<?php

// De toegestane extenties, die de file mag hebben
$extensions = array( '.jpg', '.jpeg', '.gif', '.png' );
// De toegestane MIME types
$types = array( 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif' );
// De file die geupload moet worden
$file = $_FILES['foto_kunstwerk'];
// De map waarin de foto gezet moet worden
$path = '../../uploads/';
/**
 * File controleren. Er moeten dingen worden opgegeven als parameters, deze eerst declareren
 * 
 * @param Array $extensions
 * @param Array $types
 * @param Integer $size
 */
$upload = new uploader( $extensions, $types );
/**
 * De file daadwerkelijk uploaden. De klass die hier aangekoppeld is voert alle controles uit.
 * 
 * @param String $file
 * @param String $path
 */
$upload->upload( $file, $path );

/**
 * Als er foutmelding zijn opgetreden worden die nu weergegeven met deze methode
 */
echo $upload->errors( 'upload_errors' );


?>

Reacties

0
Nog geen reacties.