(file) Return to upload_class.php CVS log (file) (dir) Up to [RizwankCVS] / geekymedia_web / photo / sspadmin / inc

  1 rizwank 1.1 <?php 
  2             /************************************************************************
  3             File upload v2.22 - 
  4             A simple class for your (multiple) file uploads
  5             
  6             Copyright (C) 2004 - 2005 - Olaf Lederer
  7             
  8             This program is free software; you can redistribute it and/or modify
  9             it under the terms of the GNU General Public License as published by
 10             the Free Software Foundation; either version 2 of the License, or
 11             (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 21             
 22 rizwank 1.1 _________________________________________________________________________
 23             available at http://www.finalwebsites.com 
 24             Comments & suggestions: http://www.finalwebsites.com/contact.php
 25             
 26             Updates:
 27             version 1.01 - added the $DOCUMENT_ROOT server variable to the $upload_dir
 28             in the example.php.
 29             
 30             version 1.02 - added the 'check_dir' method to the class. In this version 
 31             the user get an error if the defined upload-directory doesn't exsit. 
 32             
 33             version 1.10 - now the class will check if a file alreay exists. Use the
 34             replace-property to overwrite an old file. Notice the checkbox inside the 
 35             example file: Check this box to toggle the replace-property.
 36             
 37             version 2.00 - made this class more compact (removed some useless variables)
 38             and added a new method(show_extensions()) to the basic class. All messages 
 39             are saved from now in the $message property (not replaced anymore) New in 
 40             this release is the multi upload example, with an extended class is it 
 41             possible to upload more then one file at the same time.
 42             
 43 rizwank 1.1 version 2.01 - added a new method: With "check_file_name()" the class will
 44             controle if a filename uses only charcters recognized by a regular filesytem.
 45             
 46             version 2.02 - The method upload() has boolean function now. There is now
 47             a property to switch the method check_file_name() on or off. CHMOD = 0777
 48             if the property $replace is set to "y".
 49             
 50             version 2.10 - The check_file_name() method tests now the string length and 
 51             checks that the filename is not empty (I removed the file check from the upload 
 52             method). The $message property is changed into an array. This array will be 
 53             filled by the error_text() method (this method is a extended version of the 
 54             old report_error() method). Inside this method you can switch the message by the new 
 55             property $language to use translations for the output. The message output is 
 56             done by the new show_error_string() method. I did also some cleaning work and 
 57             (small) bug fixing in several methods. I changed the multi_upload_example
 58             to work with the new error reporting methodes, too.
 59             
 60             version 2.20 - I changed the upload() method to handle also unique filenames.
 61             This will happen if you set the var $rename_file on true. I added also some user
 62             friendly (dutch) messages to the main class. I isolated the function to get the file
 63             extension into a new method: function get_extension(). Notice the new extension:
 64 rizwank 1.1 "foto_upload", these are some easy to use functions to resize an rotate 
 65             images. After the thumbnail and photo creation is done there is delete method to 
 66             remove the (orig.) uploaded file.
 67             
 68             Version 2.21 - Added spanish messages to the class, thanks Sergio for the 
 69             translations.
 70             
 71             version 2.22 - I changed the class to work with configurations where 
 72             register_globals = off an added the german translations, thank you Dirk Klingenberg!
 73             *************************************************************************/
 74             
 75              
 76             class file_upload {
 77             
 78                 var $the_file;
 79             	var $the_temp_file;
 80                 var $upload_dir;
 81             	var $replace;
 82             	var $do_filename_check;
 83             	var $max_length_filename = 100;
 84                 var $extensions;
 85 rizwank 1.1 	var $ext_string;
 86             	var $language;
 87             	var $http_error;
 88             	var $rename_file; // if this var is true the file copy get a new name
 89             	var $file_copy; // the new name
 90             	var $message = array();
 91             	
 92             	function file_upload() {
 93             		$this->language = "en"; // choice of en, nl, es
 94             		$this->rename_file = false;
 95             		$this->ext_string = "";
 96             	}
 97             	// some error (HTTP)reporting, change the messages or remove options if you like.
 98             	function error_text($err_num) {
 99             		switch ($this->language) {
100             			case "nl":
101             			$error[0] = "Foto succesvol kopieert.";
102             			$error[1] = "Het bestand is te groot, controlleer de max. toegelaten bestandsgrootte.";
103             			$error[2] = "Het bestand is te groot, controlleer de max. toegelaten bestandsgrootte.";
104             			$error[3] = "Fout bij het uploaden, probeer het nog een keer.";
105             			$error[4] = "Fout bij het uploaden, probeer het nog een keer.";
106 rizwank 1.1 			$error[10] = "Selecteer een bestand.";
107             			$error[11] = "Het zijn alleen bestanden van dit type toegestaan: <b>".$this->ext_string."</b>";
108             			$error[12] = "Sorry, de bestandsnaam bevat tekens die niet zijn toegestaan. Gebruik alleen nummer, letters en het underscore teken. Een geldige naam eindigt met een punt en de extensie.";
109             			$error[13] = "De bestandsnaam is te lang, het maximum is: ".$this->max_length_filename." teken.";
110             			$error[14] = "Sorry, het opgegeven directory bestaat niet!";
111             			$error[15] = "Uploading <b>".$this->the_file."...Fout!</b> Sorry, er is al een bestand met deze naam aanwezig.";
112             			break;
113             			case "de":
114             			$error[0] = "Die Datei: <b>".$this->the_file."</b> wurde hochgeladen!"; 
115             			$error[1] = "Die hochzuladende Datei ist gr&ouml;&szlig;er als der Wert in der Server-Konfiguration!"; 
116             			$error[2] = "Die hochzuladende Datei ist gr&ouml;&szlig;er als der Wert in der Klassen-Konfiguration!"; 
117             			$error[3] = "Die hochzuladende Datei wurde nur teilweise &uuml;bertragen"; 
118             			$error[4] = "Es wurde keine Datei hochgeladen"; 
119             			$error[10] = "W&auml;hlen Sie eine Datei aus!."; 
120             			$error[11] = "Es sind nur Dateien mit folgenden Endungen erlaubt: <b>".$this->ext_string."</b>";
121             			$error[12] = "Der Dateiname enth&auml;lt ung&uuml;ltige Zeichen. Benutzen Sie nur alphanumerische Zeichen f&uuml;r den Dateinamen mit Unterstrich.Ein g&uuml;ltiger Dateiname endet mit einem Punkt, gefolgt von der Endung."; 
122             			$error[13] = "Der Dateiname &uuml;berschreitet die maximale Anzahl von ".$this->max_length_filename." Zeichen."; 
123             			$error[14] = "Das Upload-Verzeichnis existiert nicht!"; 
124             			$error[15] = "Upload <b>".$this->the_file."...Fehler!</b> Eine Datei mit gleichem Dateinamen existiert bereits.";
125             			break;
126             			case "es":
127 rizwank 1.1 			$error[0] = "El fichero: <b>".$this->the_file."</b> se ha cargado correctamente!";
128             			$error[1] = "El fichero a cargar excede del tama&ntilde;o m&aacute;ximo de la directiva en la configuraci&oacute;n del servidor.";
129             			$error[2] = "El fichero a cargar excede del tama&ntilde;o m&aacute;ximo de la directiva especificada en el formulario html.";
130             			$error[3] = "El fichero a cargar solo lo ha sido parcialmente.";
131             			$error[4] = "El fichero no ha sido cargado.";
132             			$error[10] = "Por favor seleccione un fichero a cargar.";
133             			$error[11] = "Solo ficheros con las siguientes extensiones est&aacute;n permitidos: <b>".$this->ext_string."</b>";
134             			$error[12] = "Lo siento, el nombre del fichero contiene car&aacute;cteres invalidos. Use solo car&aacute;cteres alfanum&eacute;ricos y separe (si es necesario) con un subrayado. Un nombre de fichero correcto acaba con un punto seguido de la extensi&oacute;n.";
135             			$error[13] = "El nombre de fichero excede de la longitud m&aacute;xima de ".$this->max_length_filename." car&aacute;cteres.";
136             			$error[14] = "¡Lo siento, el directorio de destino no existe!";
137             			$error[15] = "Cargando <b>".$this->the_file."...Error!</b> lo siento, un fichero con el mismo nombre ya existe.";
138             			break;
139             			default:
140             			// start http errors
141             			$error[0] = "File: <b>".$this->the_file."</b> successfully uploaded!";
142             			$error[1] = "The uploaded file exceeds the max. upload filesize directive in the server configuration.";
143             			$error[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form.";
144             			$error[3] = "The uploaded file was only partially uploaded";
145             			$error[4] = "No file was uploaded";
146             			// end  http errors
147             			$error[10] = "Please select a file for upload.";
148 rizwank 1.1 			$error[11] = "Only files with the following extensions are allowed: <b>".$this->ext_string."</b>";
149             			$error[12] = "Sorry, the filename contains invalid characters. Use only alphanumerical chars and separate parts of the name (if needed) with an underscore. A valid filename ends with one dot followed by the extension.";
150             			$error[13] = "The filename exceeds the maximum length of ".$this->max_length_filename." characters.";
151             			$error[14] = "Sorry, the upload directory doesn't exist!";
152             			$error[15] = "Uploading <b>".$this->the_file."...Error!</b> Sorry, a file with this name already exitst.";
153             			
154             		}
155             		return $error[$err_num];
156             	}
157             	function show_error_string() {
158             		$msg_string = "";
159             		foreach ($this->message as $value) {
160             			$msg_string .= $value."<br>\n";
161             		}
162             		return $msg_string;
163             	}
164             	function upload() {
165             		if ($this->check_file_name()) {
166             			if ($this->validateExtension()) {
167             				if (is_uploaded_file($this->the_temp_file)) {
168             					// this check/conversion is used for unique filenames 
169 rizwank 1.1 					$this->file_copy = ($this->rename_file) ? strtotime("now").$this->get_extension($this->the_file) : $this->the_file;
170             					if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
171             						$this->message[] = $this->error_text($this->http_error);
172             						return true;
173             					}
174             				} else {
175             					$this->message[] = $this->error_text($this->http_error);
176             					return false;
177             				}
178             			} else {
179             				$this->show_extensions();
180             				$this->message[] = $this->error_text(11);
181             				return false;
182             			}
183             		} else {
184             			return false;
185             		}
186             	}
187             	function check_file_name() {
188             		if ($this->the_file != "") {
189             			if (strlen($this->the_file) > $this->max_length_filename) {
190 rizwank 1.1 				$this->message[] = $this->error_text(13);
191             				return false;
192             			} else {
193             				if ($this->do_filename_check == "y") {
194             					if (ereg("^[a-zA-z0-9_]*\.[a-zA-az]{3,4}$", $this->the_file)) {
195             						return true;
196             					} else {
197             						$this->message[] = $this->error_text(12);
198             						return false;
199             					}
200             				} else {
201             					return true;
202             				}
203             			}
204             		} else {
205             			$this->message[] = $this->error_text(10);
206             			return false;
207             		}
208             	}
209             	function get_extension($from_file) {
210             		$ext = strtolower(strrchr($from_file,"."));
211 rizwank 1.1 		return $ext;
212             	}
213             	function validateExtension() {
214             		$extension = $this->get_extension($this->the_file);
215             		$ext_array = $this->extensions;
216             		if (in_array($extension, $ext_array)) { 
217             			return true;
218             		} else {
219             			return false;
220             		}
221             	}
222             	// this method is only used for detailed error reporting
223             	function show_extensions() {
224             		$this->ext_string = implode(" ", $this->extensions);
225             	}
226             	function move_upload($tmp_file, $new_file) {
227             		umask(0);
228             		if ($this->existing_file()) {
229             			$newfile = $this->upload_dir.$new_file;
230             			if ($this->check_dir()) {
231             				if (move_uploaded_file($tmp_file, $newfile)) {
232 rizwank 1.1 					if ($this->replace == "y") {
233             						system("chmod 0777 $newfile");
234             					} else {
235             						system("chmod 0755 $newfile");
236             					}
237             					return true;
238             				} else {
239             					return false;
240             				}
241             			} else {
242             				$this->message[] = $this->error_text(14);
243             				return false;
244             			}
245             		} else {
246             			$this->message[] = $this->error_text(15);
247             			return false;
248             		}
249             	}
250             	function check_dir() {
251             		if (!is_dir($this->upload_dir)) {
252             			return false;
253 rizwank 1.1 		} else {
254             			return true;
255             		}
256             	}
257             	function existing_file() {
258             		if ($this->replace == "y") {
259             			return true;
260             		} else {
261             			if (file_exists($this->upload_dir.$this->the_file)) {
262             				return false;
263             			} else {
264             				return true;
265             			}
266             		}
267             	}
268             }
269             ?>

Rizwan Kassim
Powered by
ViewCVS 0.9.2