34.204.169.76 |    

Navigation

Google Advertisement

Class to upload files to a server.

upload.class.php
  1. <?php
  2. /***************************************************************
  3.  * http://wiki.tuxnet24.de/
  4.  * Copyright (c) 2008 Marko Schulz <info@tuxnet24.de>
  5.  * All Rights Reserved.
  6.  *
  7.  * --LICENSE NOTICE--
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  * --LICENSE NOTICE--
  22.  ***************************************************************
  23.  * $File: upload.class.php $
  24.  * $Author: mschulz $ - $Date: 2006/12/20 20:44:20 $
  25.  * $Source: /var/lib/cvs/php-001/classes/upload.class.php,v $ - $Revision: 1.1 $
  26.  * $Description: Class to upload files to a server. $
  27.  * $Copyright: (c) 2008 Marko Schulz - <info@tuxnet24.de> $
  28.  ***************************************************************/
  29.  
  30. class Upload {
  31.  
  32. 	public $filename;
  33. 	public $filetype;
  34. 	public $filesize;
  35.  
  36. 	private $max;
  37. 	private $size;
  38. 	private $error;
  39. 	private $directory;
  40. 	private $filetemp;
  41. 	private $whitelist;
  42. 	private $overwrite;
  43. 	private $maxsize;
  44.  
  45. 	/********************************************************
  46. 	 * This method to initialize the uploded file.
  47. 	 *
  48. 	 *	(object) new Upload( array( 'filename' => (string) $_FILES['file']['name'],
  49. 	 *								'filetemp' => (string) $_FILES['file']['tmp_name'],
  50. 	 *								'filetype' => (string) $_FILES['file']['type'],
  51. 	 *								'filesize' => (integer) $_FILES['file']['size'],
  52. 	 *								'directory' => (string) $directory
  53. 	 *								[ , 'error' => (integer) $_FILES['file']['error'],
  54. 	 *								'whitelist' => (array) $filetype,
  55. 	 *								'overwrite' => (string) $overwrite,
  56. 	 *								'maxsize' => (integer) $maxsize ] ) );
  57. 	 ********************************************************/
  58. 	public function Upload ( $args ) {
  59.  
  60. 		// values from $_FILE array...
  61. 		$this->filename = $args['filename'] ? $args['filename'] : NULL; // required...
  62. 		$this->filetemp = $args['filetemp'] ? $args['filetemp'] : NULL; // required...
  63. 		$this->filetype = $args['filetype'] ? $args['filetype'] : NULL; // required...
  64. 		$this->filesize = $args['filesize'] ? $args['filesize'] : NULL; // required...
  65. 		$this->error = $args['error'] ? $args['error'] : 0;             // optional...
  66.  
  67. 		// user defined values...
  68. 		$this->overwrite = $args['overwrite'] ? $args['overwrite'] : 0;    // optional...
  69. 		$this->directory = $args['directory'] ? $args['directory'] : NULL; // required...
  70. 		$this->whitelist = $args['whitelist'] ? $args['whitelist'] : NULL; // optional...
  71. 		$this->maxsize = $args['maxsize'] ? $args['maxsize'] : NULL;       // optional...
  72.  
  73. 		// get an error if required parameters missing...
  74. 		if ( !is_file( $this->filetemp ) ) die( "No temporary file to upload!" );
  75. 		if ( !$this->filename ) die( "No filename defined to upload!" );
  76. 		if ( !$this->filetype ) die( "No filetype defined to upload!" );
  77. 		if ( $this->filesize == 0 ) die( $this->filename." is a 0 byte file!" );
  78.  
  79. 		// check if upload directory exists and is writable...
  80. 		if ( !$this->directory || !$this->CheckDirectory() )
  81. 			die( "Upload directory doesn't exits or is not writable!" );
  82.  
  83. 		// check system settings...
  84. 		if ( !ini_get( 'file_uploads' ) ) die( "No file upload allowed in php.ini!" );
  85. 		if ( $this->maxsize && $this->maxsize > $this->GetBytes( ini_get( 'upload_max_filesize' ) ) )
  86. 			die( "Your maxsize is greater than upload_max_filesize in php.ini!" );
  87.  
  88. 		// get the last part of the mimetype...
  89. 		$this->extension = array_pop( explode( '.', $this->filename ) );
  90.  
  91. 	}
  92.  
  93. 	/********************************************************
  94. 	 * This method execute the upload.
  95. 	 *
  96. 	 *	(string) $object->GetUpload();
  97. 	 ********************************************************/
  98. 	public function GetUpload () {
  99.  
  100. 		if ( $this->maxsize ) // check if file size greater than the defined maxsize...
  101. 			if ( !$this->CheckFilesize( $this->maxsize ) ) return 'ERR_MAXSIZE';
  102.  
  103. 		// check if file size greater than upload_max_filesize in php.ini...
  104. 		if ( $this->filesize > $this->GetBytes( ini_get( 'upload_max_filesize' ) ) ) return 'ERR_UPLOADSIZE';
  105.  
  106. 		if ( $this->whitelist ) // check if file extension is allowed...
  107. 			if ( !$this->CheckFiletype() ) return 'ERR_EXTENTION';
  108.  
  109. 		if ( $this->overwrite == 0 ) // check if upload file already exists...
  110. 			if ( !$this->CheckExists() ) return 'ERR_OVERWRITE';
  111.  
  112. 		// finish upload - move temporary uploaded file to filename...
  113. 		if ( !@move_uploaded_file( $this->filetemp, $this->directory.'/'.$this->filename ) ) return 'ERR_UNSAVE';
  114.  
  115. 		return NULL;
  116.  
  117. 	}
  118.  
  119. 	/********************************************************
  120. 	 * This method check if the upload directory exists
  121. 	 * and is writable and return true or false.
  122. 	 *
  123. 	 *	(bool) $this->CheckExists();
  124. 	 ********************************************************/
  125. 	private function CheckDirectory () {
  126. 		if ( !is_writable( $this->directory ) ) return false;
  127. 		else return true;
  128. 	}
  129.  
  130. 	/********************************************************
  131. 	 * This method check the file size and return true or false.
  132. 	 *
  133. 	 *	(bool) $this->CheckFilesize( (integer) $max );
  134. 	 ********************************************************/
  135. 	private function CheckFilesize ( $max ) {
  136. 		if ( $this->filesize > $max ) return false;
  137. 		else return true;
  138. 	}
  139.  
  140. 	/********************************************************
  141. 	 * This method check the right file extension and return true or false.
  142. 	 *
  143. 	 *	(bool) $this->CheckFiletype();
  144. 	 ********************************************************/
  145. 	private function CheckFiletype () {
  146. 		if ( !in_array( strtolower($this->extension), $this->whitelist ) ) return false;
  147. 		else return true;
  148. 	}
  149.  
  150. 	/********************************************************
  151. 	 * This method check if the file already exists and return true or false.
  152. 	 *
  153. 	 *	(bool) $this->CheckExists();
  154. 	 ********************************************************/
  155. 	private function CheckExists () {
  156. 		if ( is_file( $this->directory.'/'.$this->filename ) ) return false;
  157. 		else return true;
  158. 	}
  159.  
  160. 	/********************************************************
  161. 	 * This method get the filesize in bytes from upload_max_filesize.
  162. 	 *
  163. 	 *	(integer) $this->GetBytes( (mixed) $size );
  164. 	 ********************************************************/
  165. 	private function GetBytes( $size ) {
  166.  
  167. 		if ( is_numeric( $size ) ) return $size;
  168. 		else {
  169. 			(integer) $length = strlen( $size );
  170. 			(integer) $byte = substr( $size, 0, $length - 1 );
  171. 			(string)  $unit = strtolower( substr( $size, $length - 1 ) );
  172.  
  173. 			switch ( $unit ) {
  174. 				case 'k':
  175. 					$byte *= 1024;
  176. 					break;
  177. 				case 'm':
  178. 					$byte *= 1048576;
  179. 					break;
  180. 				case 'g':
  181. 					$byte *= 1073741824;
  182. 					break;
  183. 			}
  184.  
  185. 			return $byte;
  186.  
  187. 		}
  188. 	}
  189.  
  190. };
  191.  
  192. // *************************************************************
  193. // EOF
  194.  
  195. ?>
Parsed in 0.025 seconds at 291.76 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 3 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)