PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/include/engine/core/iwFileUpload.class.php

https://bitbucket.org/k_shehadeh/filenexus
PHP | 690 lines | 327 code | 62 blank | 301 comment | 64 complexity | dfe92e2d6fc899442e5ae3655ad6423f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. *
  4. * class iwFileUpload
  5. *
  6. * Copyright 1999, 2002, 2003 David Fox, Dave Tufts
  7. * Language specific error messaging:
  8. * [fr] Frank from http://www.ibigin.com - initial code and French text
  9. * [de] lmg from http://www.kishalmi.net - German text
  10. * [nl] Andre, a.t.somers@student.utwente.nl - Dutch text
  11. * [it] Enrico Valsecchi http://www.hostyle.it <admin@hostyle.it> - Italian text
  12. * [fi] Dotcom Media Solutions, http://www.dotcom.ms - Finnish text
  13. * [es] Alejandro Ramirez <alex@cinenganos.com> - Spanish text
  14. * [no] Sigbjorn Eide <seide@tiscali.no> - Norwegian text
  15. * [da] Thomas Hannibal http://hannibalsoftware.dk/ - Danish Text
  16. *
  17. * Usage, setup, and license at the bottom of this page (README)
  18. *
  19. * @version: 2.15
  20. * @last_update: 2004-02-18
  21. * @description: PHP file upload class
  22. * @requires: PHP 4.1 or higher
  23. *
  24. * @changes: v2.15 - Added Danish (da) error messaging
  25. * @changes: v2.14 - Edited acceptable_file_types checks to be more lenient
  26. * @changes: v2.13 - Added Spanish (es) and Norwegian (no) error messaging, converted all non-valid HTML language chars to named entities
  27. * @changes: v2.12 - Added Finnish (fi) error messaging
  28. * @changes: v2.11 - Fixed bug if $this->save_file::$path is ""
  29. * @changes: v2.10 - Added var $path to class definition
  30. * @changes: v2.9 - Updated error_message[5] for NL (Dutch)
  31. * @changes: v2.8 - Cleaned up Italian error messaging (thanks to Maurizio Lemmo - http://www.tenzione.it/ )
  32. * @changes: v2.7 - Added new error code [5] to save_file() method, fixed minor bug if unable to write to upload directory
  33. * @changes: v2.6 - Added $this->acceptable_file_types. Fixed minor bug fix in upload() - if file 'type' is null
  34. * @changes: v2.5.2 - Added Italian (it) error messgaing
  35. * @changes: v2.5.1 - Added German (de) and Dutch (nl) error messgaing
  36. * @changes: v2.4 - Added error messgae language preferences
  37. * @changes: v2.3.1 - Bugfix for upload $path in $this->save_file()
  38. * @changes: v2.3 - Initialized all variables (compatibale with PHP error notices)
  39. * @changes: v2.2 - Changed ereg() to stristr() whenever possible
  40. *
  41. *
  42. * METHODS:
  43. * iwFileUpload() - constructor, sets error message language preference
  44. * max_filesize() - set a max filesize in bytes
  45. * max_image_size() - set max pixel dimenstions for image uploads
  46. * upload() - checks if file is acceptable, uploads file to server's temp directory
  47. * save_file() - moves the uploaded file and renames it depending on the save_file($overwrite_mode)
  48. *
  49. * cleanup_text_file() - (PRIVATE) convert Mac and/or PC line breaks to UNIX
  50. * get_error() - (PRIVATE) gets language-specific error message
  51. *
  52. * Error code: available in English (en), French (fr), German (de), Dutch (nl), Italian (it)
  53. * [0] - "No file was uploaded"
  54. * [1] - "Maximum file size exceeded"
  55. * [2] - "Maximum image size exceeded"
  56. * [3] - "Only specified file type may be uploaded"
  57. * [4] - "File already exists" (save only)
  58. * [5] - "Permission denied. Unable to copy file"
  59. *
  60. */
  61. class iwFileUpload {
  62. var $file;
  63. var $path;
  64. var $language;
  65. var $acceptable_file_types;
  66. var $error;
  67. var $errors; // Depreciated (only for backward compatability)
  68. var $accepted;
  69. var $max_filesize;
  70. var $max_image_width;
  71. var $max_image_height;
  72. /**
  73. * object iwFileUpload ([string language]);
  74. *
  75. * Class constructor, sets error messaging language preference
  76. *
  77. * @param language (string) defaults to en (English).
  78. *
  79. * @examples: $f = new iwFileUpload(); // English error messages
  80. * $f = new iwFileUpload('fr'); // French error messages
  81. * $f = new iwFileUpload('de'); // German error messages
  82. * $f = new iwFileUpload('nl'); // Dutch error messages
  83. * $f = new iwFileUpload('it'); // Italian error messages
  84. * $f = new iwFileUpload('fi'); // Finnish error messages
  85. * $f = new iwFileUpload('es'); // Spanish error messages
  86. * $f = new iwFileUpload('no'); // Norwegian error messages
  87. * $f = new iwFileUpload('da'); // Danish error messages
  88. *
  89. */
  90. function iwFileUpload ( $language = 'en' ) {
  91. $this->language = strtolower($language);
  92. $this->error = '';
  93. }
  94. /**
  95. * void max_filesize ( int size);
  96. *
  97. * Set the maximum file size in bytes ($size), allowable by the object.
  98. * NOTE: PHP's configuration file also can control the maximum upload size, which is set to 2 or 4
  99. * megs by default. To upload larger files, you'll have to change the php.ini file first.
  100. *
  101. * @param size (int) file size in bytes
  102. *
  103. */
  104. function max_filesize($size){
  105. $this->max_filesize = (int) $size;
  106. }
  107. /**
  108. * void max_image_size ( int width, int height );
  109. *
  110. * Sets the maximum pixel dimensions. Will only be checked if the
  111. * uploaded file is an image
  112. *
  113. * @param width (int) maximum pixel width of image uploads
  114. * @param height (int) maximum pixel height of image uploads
  115. *
  116. */
  117. function max_image_size($width, $height){
  118. $this->max_image_width = (int) $width;
  119. $this->max_image_height = (int) $height;
  120. }
  121. /** Retrieves the mime type for the uploaded file
  122. *
  123. * @return string A mime type value or empty if there was an error.
  124. */
  125. function getFilePath () {
  126. return $this->file ['tmp_name'];
  127. }
  128. /** Retrieves the mime type for the uploaded file
  129. *
  130. * @return string A mime type value or empty if there was an error.
  131. */
  132. function getFileType () {
  133. return $this->file ['type'];
  134. }
  135. /** Retrieves the contents of the file as a slashed string
  136. *
  137. * The buffer of the file is returned in a database friendly string
  138. *
  139. * @return string If successful, the contents of the file as a string, otherwise an empty string.
  140. * @access public
  141. */
  142. function getFileBuffer () {
  143. $buffer = '';
  144. // Clean up text file breaks
  145. if(stristr($this->file["type"], "text")) {
  146. $this->cleanup_text_file($this->file["tmp_name"]);
  147. }
  148. //***********************************
  149. // Modified Code from php.net (rynop2000)
  150. //***********************************
  151. $filehandle = fopen($this->file["tmp_name"], "rb");
  152. $filesize = filesize($this->file["tmp_name"]);
  153. if ($filesize > 0) {
  154. if (get_magic_quotes_gpc () == true) {
  155. // The system is set to automatically quote
  156. // data being sent to the database so we
  157. // don't need to add slashes here.
  158. $buffer = fread($filehandle, $filesize);
  159. }
  160. else {
  161. // We have to manually handle quotes in
  162. // the data buffer by adding slashes. So
  163. // this adds slashes when data is retrieved.
  164. $buffer = addslashes (fread($filehandle, $filesize));
  165. }
  166. }
  167. fclose ($filehandle);
  168. return $buffer;
  169. }
  170. /**
  171. * Loads the file into the
  172. * uploaded file is an image
  173. *
  174. * @param string $table The name of the db table
  175. * @param string $fieldName The name of the field to save into
  176. * @param string $whereClause SQL string to limit the modified records (e.g. "WHERE id=4")
  177. *
  178. */
  179. function save_to_mysql ($table, $dataFieldName, $typeFieldName, $whereClause) {
  180. // Clean up text file breaks
  181. if(stristr($this->file["type"], "text")) {
  182. $this->cleanup_text_file($this->file["tmp_name"]);
  183. }
  184. //***********************************
  185. // Modified Code from php.net (rynop2000)
  186. //***********************************
  187. $filehandle = fopen($this->file["tmp_name"], "rb");
  188. $filesize = filesize($this->file["tmp_name"]);
  189. // Clear the field first.
  190. $query = "UPDATE $table SET $dataFieldName = '' $whereClause";
  191. if ($filesize > 0) {
  192. $buffer = addslashes (fread($filehandle, $filesize));
  193. $query = "UPDATE $table SET $dataFieldName = '$buffer' $whereClause";
  194. $result = mysql_query($query);
  195. if (mysql_affected_rows () == 0) {
  196. if (mysql_error () != '') {
  197. $this->error = mysql_error();
  198. fclose ($filehandle);
  199. return false;
  200. }
  201. }
  202. }
  203. fclose ($filehandle);
  204. // Now we can set the data type
  205. $query = "UPDATE $table SET $typeFieldName = '".$this->file["type"]."' $whereClause";
  206. mysql_query($query);
  207. if (mysql_affected_rows () == 0) {
  208. if (mysql_error () != '') {
  209. $this->error = sprintf (STR_46, mysql_error());
  210. return false;
  211. }
  212. }
  213. return true;
  214. }
  215. /**
  216. * bool upload (string filename[, string accept_type[, string extension]]);
  217. *
  218. * Checks if the file is acceptable and uploads it to PHP's default upload diretory
  219. *
  220. * @param filename (string) form field name of uploaded file
  221. * @param accept_type (string) acceptable mime-types
  222. * @param extension (string) default filename extenstion
  223. *
  224. */
  225. function upload($filename='', $accept_type='', $extention='') {
  226. $this->acceptable_file_types = trim($accept_type); // used by error messages
  227. if (!isset($_FILES) || !is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) {
  228. $this->error = $this->get_error(0);
  229. $this->accepted = FALSE;
  230. return FALSE;
  231. }
  232. // Copy PHP's global $_FILES array to a local array
  233. $this->file = $_FILES[$filename];
  234. $this->file['file'] = $filename;
  235. // Initialize empty array elements
  236. if (!isset($this->file['extention'])) $this->file['extention'] = "";
  237. if (!isset($this->file['type'])) $this->file['type'] = "";
  238. if (!isset($this->file['size'])) $this->file['size'] = "";
  239. if (!isset($this->file['width'])) $this->file['width'] = "";
  240. if (!isset($this->file['height'])) $this->file['height'] = "";
  241. if (!isset($this->file['tmp_name'])) $this->file['tmp_name'] = "";
  242. if (!isset($this->file['raw_name'])) $this->file['raw_name'] = "";
  243. // test max size
  244. if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) {
  245. $this->error = $this->get_error(1);
  246. $this->accepted = FALSE;
  247. return FALSE;
  248. }
  249. if(stristr($this->file["type"], "image")) {
  250. /* IMAGES */
  251. $image = getimagesize($this->file["tmp_name"]);
  252. $this->file["width"] = $image[0];
  253. $this->file["height"] = $image[1];
  254. // test max image size
  255. if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
  256. $this->error = $this->get_error(2);
  257. $this->accepted = FALSE;
  258. return FALSE;
  259. }
  260. // Image Type is returned from getimagesize() function
  261. switch($image[2]) {
  262. case 1:
  263. $this->file["extention"] = ".gif"; break;
  264. case 2:
  265. $this->file["extention"] = ".jpg"; break;
  266. case 3:
  267. $this->file["extention"] = ".png"; break;
  268. case 4:
  269. $this->file["extention"] = ".swf"; break;
  270. case 5:
  271. $this->file["extention"] = ".psd"; break;
  272. case 6:
  273. $this->file["extention"] = ".bmp"; break;
  274. case 7:
  275. $this->file["extention"] = ".tif"; break;
  276. case 8:
  277. $this->file["extention"] = ".tif"; break;
  278. default:
  279. $this->file["extention"] = $extention; break;
  280. }
  281. } elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
  282. // Try and autmatically figure out the file type
  283. // For more on mime-types: http://httpd.apache.org/docs/mod/mod_mime_magic.html
  284. switch($this->file["type"]) {
  285. case "text/plain":
  286. $this->file["extention"] = ".txt"; break;
  287. case "text/richtext":
  288. $this->file["extention"] = ".txt"; break;
  289. default:
  290. break;
  291. }
  292. } else {
  293. $this->file["extention"] = $extention;
  294. }
  295. // check to see if the file is of type specified
  296. if($this->acceptable_file_types) {
  297. if(trim($this->file["type"]) && (stristr($this->acceptable_file_types, $this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) {
  298. $this->accepted = TRUE;
  299. } else {
  300. $this->accepted = FALSE;
  301. $this->error = $this->get_error(3);
  302. }
  303. } else {
  304. $this->accepted = TRUE;
  305. }
  306. return (bool) $this->accepted;
  307. }
  308. /**
  309. * bool save_file ( string path[, int overwrite_mode] );
  310. *
  311. * Cleans up the filename, copies the file from PHP's temp location to $path,
  312. * and checks the overwrite_mode
  313. *
  314. * @param path (string) File path to your upload directory
  315. * @param overwrite_mode (int) 1 = overwrite existing file
  316. * 2 = rename if filename already exists (file.txt becomes file_copy0.txt)
  317. * 3 = do nothing if a file exists
  318. *
  319. */
  320. function save_file($path, $overwrite_mode="3"){
  321. if ($this->error) {
  322. return false;
  323. }
  324. if (strlen($path)>0) {
  325. if ($path[strlen($path)-1] != "/") {
  326. $path = $path . "/";
  327. }
  328. }
  329. $this->path = $path;
  330. $copy = "";
  331. $n = 1;
  332. $success = false;
  333. if($this->accepted) {
  334. // Clean up file name (only lowercase letters, numbers and underscores)
  335. $this->file["name"] = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($this->file["name"]))));
  336. // Clean up text file breaks
  337. if(stristr($this->file["type"], "text")) {
  338. $this->cleanup_text_file($this->file["tmp_name"]);
  339. }
  340. // get the raw name of the file (without its extenstion)
  341. if(ereg("(\.)([a-z0-9]{2,5})$", $this->file["name"])) {
  342. $pos = strrpos($this->file["name"], ".");
  343. if(!$this->file["extention"]) {
  344. $this->file["extention"] = substr($this->file["name"], $pos, strlen($this->file["name"]));
  345. }
  346. $this->file['raw_name'] = substr($this->file["name"], 0, $pos);
  347. } else {
  348. $this->file['raw_name'] = $this->file["name"];
  349. if ($this->file["extention"]) {
  350. $this->file["name"] = $this->file["name"] . $this->file["extention"];
  351. }
  352. }
  353. switch((int) $overwrite_mode) {
  354. case 1: // overwrite mode
  355. if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
  356. $success = true;
  357. } else {
  358. $success = false;
  359. $this->error = $this->get_error(5);
  360. }
  361. break;
  362. case 2: // create new with incremental extention
  363. while(file_exists($this->path . $this->file['raw_name'] . $copy . $this->file["extention"])) {
  364. $copy = "_copy" . $n;
  365. $n++;
  366. }
  367. $this->file["name"] = $this->file['raw_name'] . $copy . $this->file["extention"];
  368. if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
  369. $success = true;
  370. } else {
  371. $success = false;
  372. $this->error = $this->get_error(5);
  373. }
  374. break;
  375. default: // do nothing if exists, highest protection
  376. if(file_exists($this->path . $this->file["name"])){
  377. $this->error = $this->get_error(4);
  378. $success = false;
  379. } else {
  380. if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
  381. $success = true;
  382. } else {
  383. $success = false;
  384. $this->error = $this->get_error(5);
  385. }
  386. }
  387. break;
  388. }
  389. if(!$success) { unset($this->file['tmp_name']); }
  390. return (bool) $success;
  391. } else {
  392. $this->error = $this->get_error(3);
  393. return FALSE;
  394. }
  395. }
  396. /**
  397. * string get_error(int error_code);
  398. *
  399. * Gets the correct error message for language set by constructor
  400. *
  401. * @param error_code (int) error code
  402. *
  403. */
  404. function get_error($error_code='') {
  405. $error_message = array();
  406. $error_code = (int) $error_code;
  407. switch ( $this->language ) {
  408. // French (fr)
  409. case 'fr':
  410. $error_message[0] = "Aucun fichier n'a &eacute;t&eacute; envoy&eacute;";
  411. $error_message[1] = "Taille maximale autoris&eacute;e d&eacute;pass&eacute;e. Le fichier ne doit pas &ecirc;tre plus gros que " . $this->max_filesize/1000 . " Ko (" . $this->max_filesize . " octets).";
  412. $error_message[2] = "Taille de l'image incorrecte. L'image ne doit pas d&eacute;passer " . $this->max_image_width . " pixels de large sur " . $this->max_image_height . " de haut.";
  413. $error_message[3] = "Type de fichier incorrect. Seulement les fichiers de type " . str_replace("|", " or ", $this->acceptable_file_types) . " sont autoris&eacute;s.";
  414. $error_message[4] = "Fichier '" . $this->path . $this->file["name"] . "' d&eacute;j&aacute; existant, &eacute;crasement interdit.";
  415. $error_message[5] = "La permission a ni&eacute;. Incapable pour copier le fichier &aacute; '" . $this->path . "'";
  416. break;
  417. // German (de)
  418. case 'de':
  419. $error_message[0] = "Es wurde keine Datei hochgeladen";
  420. $error_message[1] = "Maximale Dateigr&ouml;sse &uuml;berschritten. Datei darf nicht gr&ouml;sser als " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes) sein.";
  421. $error_message[2] = "Maximale Bildgr&ouml;sse &uuml;berschritten. Bild darf nicht gr&ouml;sser als " . $this->max_image_width . " x " . $this->max_image_height . " pixel sein.";
  422. $error_message[3] = "Nur " . str_replace("|", " oder ", $this->acceptable_file_types) . " Dateien d&uuml;rfen hochgeladen werden.";
  423. $error_message[4] = "Datei '" . $this->path . $this->file["name"] . "' existiert bereits.";
  424. $error_message[5] = "Erlaubnis hat verweigert. Unf&amul;hig, Akte zu '" . $this->path . "'";
  425. break;
  426. // Dutch (nl)
  427. case 'nl':
  428. $error_message[0] = "Er is geen bestand geupload";
  429. $error_message[1] = "Maximum bestandslimiet overschreden. Bestanden mogen niet groter zijn dan " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes).";
  430. $error_message[2] = "Maximum plaatje omvang overschreven. Plaatjes mogen niet groter zijn dan " . $this->max_image_width . " x " . $this->max_image_height . " pixels.";
  431. $error_message[3] = "Alleen " . str_replace("|", " of ", $this->acceptable_file_types) . " bestanden mogen worden geupload.";
  432. $error_message[4] = "Bestand '" . $this->path . $this->file["name"] . "' bestaat al.";
  433. $error_message[5] = "Toestemming is geweigerd. Kon het bestand niet naar '" . $this->path . "' copieren.";
  434. //$error_message[5] = "Toestemming ontkende. Onbekwaam dossier aan '" . $this->path . "'";
  435. break;
  436. // Italian (it)
  437. case 'it':
  438. $error_message[0] = "Il file non e' stato salvato";
  439. $error_message[1] = "Il file e' troppo grande. La dimensione massima del file e' " . $this->max_filesize/1000 . " Kb (" . $this->max_filesize . " bytes).";
  440. $error_message[2] = "L'immagine e' troppo grande. Le dimensioni massime non possono essere superiori a " . $this->max_image_width . " pixel di larghezza per " . $this->max_image_height . " d'altezza.";
  441. $error_message[3] = "Il tipo di file non e' valido. Solo file di tipo " . str_replace("|", " o ", $this->acceptable_file_types) . " sono autorizzati.";
  442. $error_message[4] = "E' gia' presente un file con nome " . $this->path . $this->file["name"];
  443. $error_message[5] = "Permesso negato. Impossibile copiare il file in '" . $this->path . "'";
  444. break;
  445. // Finnish
  446. case 'fi':
  447. $error_message[0] = "Tiedostoa ei l&amul;hetetty.";
  448. $error_message[1] = "Tiedosto on liian suuri. Tiedoston koko ei saa olla yli " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " tavua).";
  449. $error_message[2] = "Kuva on liian iso. Kuva ei saa olla yli " . $this->max_image_width . " x " . $this->max_image_height . " pikseli&amul;.";
  450. $error_message[3] = "Vain " . str_replace("|", " tai ", $this->acceptable_file_types) . " tiedostoja voi tallentaa kuvapankkiin.";
  451. $error_message[4] = "Tiedosto '" . $this->path . $this->file["name"] . "' on jo olemassa.";
  452. $error_message[5] = "Ei k&amul;ytt&ouml;oikeutta. Tiedostoa ei voi kopioida hakemistoon '" . $this->path . "'";
  453. break;
  454. // Spanish
  455. case 'es':
  456. $error_message[0] = "No se subi&oacute; ning&uacute;n archivo.";
  457. $error_message[1] = "Se excedi&oacute; el tama&ntilde;o m&aacute;ximo del archivo. El archivo no puede ser mayor a " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes).";
  458. $error_message[2] = "Se excedieron las dimensiones de la imagen. La imagen no puede medir m&aacute;s de " . $this->max_image_width . " (w) x " . $this->max_image_height . " (h) pixeles.";
  459. $error_message[3] = "El tipo de archivo no es v&aacute;lido. S&oacute;lo los archivos " . str_replace("|", " o ", $this->acceptable_file_types) . " son permitidos.";
  460. $error_message[4] = "El archivo '" . $this->path . $this->file["name"] . "' ya existe.";
  461. $error_message[5] = "Permiso denegado. No es posible copiar el archivo a '" . $this->path . "'";
  462. break;
  463. // Norwegian
  464. case 'no':
  465. $error_message[0] = "Ingen fil ble lastet opp.";
  466. $error_message[1] = "Max filst&oslash;rrelse ble oversteget. Filen kan ikke vžre st&oslash;rre ennn " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " byte).";
  467. $error_message[2] = "Max bildest&oslash;rrelse ble oversteget. Bildet kan ikke vžre st&oslash;rre enn " . $this->max_image_width . " x " . $this->max_image_height . " piksler.";
  468. $error_message[3] = "Bare " . str_replace("|", " tai ", $this->acceptable_file_types) . " kan lastes opp.";
  469. $error_message[4] = "Filen '" . $this->path . $this->file["name"] . "' finnes fra f&oslash;r.";
  470. $error_message[5] = "Tilgang nektet. Kan ikke kopiere filen til '" . $this->path . "'";
  471. break;
  472. // Danish
  473. case 'da':
  474. $error_message[0] = "Ingen fil blev uploaded";
  475. $error_message[1] = "Den maksimale filstżrrelse er overskredet. Filerne mŒ ikke vžre stżrre end " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes).";
  476. $error_message[2] = "Den maksimale billedstżrrelse er overskredet. Billeder mŒ ikke vžre stżrre end " . $this->max_image_width . " x " . $this->max_image_height . " pixels.";
  477. $error_message[3] = "Kun " . str_replace("|", " or ", $this->acceptable_file_types) . " kan uploades.";
  478. $error_message[4] = "Filen '" . $this->path . $this->file["name"] . "' eksisterer allerede.";
  479. $error_message[5] = "Adgang nžgtet! Er ikke i stand til at kopiere filen til '" . $this->path . "'";
  480. break;
  481. // English
  482. default:
  483. $error_message[0] = "No file was uploaded";
  484. $error_message[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes).";
  485. $error_message[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels.";
  486. $error_message[3] = "Only " . str_replace("|", " or ", $this->acceptable_file_types) . " files may be uploaded.";
  487. $error_message[4] = "File '" . $this->path . $this->file["name"] . "' already exists.";
  488. $error_message[5] = "Permission denied. Unable to copy file to '" . $this->path . "'";
  489. break;
  490. }
  491. // for backward compatability:
  492. $this->errors[$error_code] = $error_message[$error_code];
  493. return $error_message[$error_code];
  494. }
  495. /**
  496. * void cleanup_text_file (string file);
  497. *
  498. * Convert Mac and/or PC line breaks to UNIX by opening
  499. * and rewriting the file on the server
  500. *
  501. * @param file (string) Path and name of text file
  502. *
  503. */
  504. function cleanup_text_file($file){
  505. // chr(13) = CR (carridge return) = Macintosh
  506. // chr(10) = LF (line feed) = Unix
  507. // Win line break = CRLF
  508. $new_file = '';
  509. $old_file = '';
  510. $fcontents = file($file);
  511. while (list ($line_num, $line) = each($fcontents)) {
  512. $old_file .= $line;
  513. $new_file .= str_replace(chr(13), chr(10), $line);
  514. }
  515. if ($old_file != $new_file) {
  516. // Open the uploaded file, and re-write it with the new changes
  517. $fp = fopen($file, "w");
  518. fwrite($fp, $new_file);
  519. fclose($fp);
  520. }
  521. }
  522. }
  523. /*
  524. <readme>
  525. fileupload-class.php can be used to upload files of any type
  526. to a web server using a web browser. The uploaded file's name will
  527. get cleaned up - special characters will be deleted, and spaces
  528. get replaced with underscores, and moved to a specified
  529. directory (on your server). fileupload-class.php also does its best to
  530. determine the file's type (text, GIF, JPEG, etc). If the user
  531. has named the file with the correct extension (.txt, .gif, etc),
  532. then the class will use that, but if the user tries to upload
  533. an extensionless file, PHP does can identify text, gif, jpeg,
  534. and png files for you. As a last resort, if there is no
  535. specified extension, and PHP can not determine the type, you
  536. can set a default extension to be added.
  537. SETUP:
  538. Make sure that the directory that you plan on uploading
  539. files to has enough permissions for your web server to
  540. write/upload to it. (usually, this means making it world writable)
  541. - cd /your/web/dir
  542. - chmod 777 <fileupload_dir>
  543. The HTML FORM used to upload the file should look like this:
  544. <form method="post" action="upload.php" enctype="multipart/form-data">
  545. <input type="file" name="userfile">
  546. <input type="submit" value="Submit">
  547. </form>
  548. USAGE:
  549. // Create a new instance of the class
  550. $my_uploader = new iwFileUpload;
  551. // OPTIONAL: set the max filesize of uploadable files in bytes
  552. $my_uploader->max_filesize(90000);
  553. // OPTIONAL: if you're uploading images, you can set the max pixel dimensions
  554. $my_uploader->max_image_size(150, 300); // max_image_size($width, $height)
  555. // UPLOAD the file
  556. $my_uploader->upload("userfile", "", ".jpg");
  557. // MOVE THE FILE to its final destination
  558. // $mode = 1 :: overwrite existing file
  559. // $mode = 2 :: rename new file if a file
  560. // with the same name already
  561. // exists: file.txt becomes file_copy0.txt
  562. // $mode = 3 :: do nothing if a file with the
  563. // same name already exists
  564. $my_uploader->save_file("/your/web/dir/fileupload_dir", int $mode);
  565. // Check if everything worked
  566. if ($my_uploader->error) {
  567. echo $my_uploader->error . "<br>";
  568. } else {
  569. // Successful upload!
  570. $file_name = $my_uploader->file['name'];
  571. print($file_name . " was successfully uploaded!");
  572. }
  573. </readme>
  574. <license>
  575. ///// fileupload-class.php /////
  576. Copyright (c) 1999, 2002, 2003 David Fox, Angryrobot Productions
  577. All rights reserved.
  578. Redistribution and use in source and binary forms, with or without
  579. modification, are permitted provided that the following conditions
  580. are met:
  581. 1. Redistributions of source code must retain the above copyright
  582. notice, this list of conditions and the following disclaimer.
  583. 2. Redistributions in binary form must reproduce the above
  584. copyright notice, this list of conditions and the following
  585. disclaimer in the documentation and/or other materials provided
  586. with the distribution.
  587. 3. Neither the name of author nor the names of its contributors
  588. may be used to endorse or promote products derived from this
  589. software without specific prior written permission.
  590. DISCLAIMER:
  591. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS"
  592. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  593. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  594. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
  595. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  596. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  597. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  598. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  599. AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  600. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  601. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  602. THE POSSIBILITY OF SUCH DAMAGE.
  603. </license>
  604. */
  605. ?>