Changeset 20 for trunk/lib/Upload.inc.php
- Timestamp:
- Nov 17, 2005 3:00:00 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Upload.inc.php
r19 r20 3 3 * Upload.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 */ 6 7 /** 5 * 8 6 * The Upload class provides an interface to deal with http uploaded files. 9 7 * 10 8 * @author Quinn Comendant <quinn@strangecode.com> 11 9 * @requires App.inc.php 12 * @version 1. 010 * @version 1.2 13 11 */ 14 12 … … 19 17 // General object parameters. 20 18 var $_params = array( 19 20 // Display message with raiseMsg? 21 21 'display_messages' => true, 22 23 // Existing files will be overwritten when there is a name conflict? 22 24 'allow_overwriting' => false, 25 26 // The filesystem path to the final upload directory. 27 'upload_path' => null, 28 29 // The file permissions of the uploaded files. Remember, files will be owned by the web server user. 30 'dest_file_perms' => 0600, 31 32 // Require file to have one of the following file extentions. 33 'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm'), 23 34 ); 24 25 // Disk path where new image(s) will be uploaded.26 var $upload_directory_path = null;27 35 28 36 // Array of files with errors. 29 37 var $errors = array(); 30 31 // Array of acceptable file extensions (lowercase). 32 var $valid_file_extensions = array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm'); 33 34 // The uploaded files will normally be owned by user 'apache'. Set world-read/write 35 // if the website admin needs to read/delete these files. 36 var $dest_file_perms = 0600; 37 38 38 39 // Array of file extensions and corresponding mime-types. 39 40 var $mime_extension_map = array( … … 223 224 { 224 225 if (isset($params) && is_array($params)) { 226 227 // Enforce valid upload_path parameter. 228 if (isset($params['upload_path'])) { 229 $params['upload_path'] = realpath($params['upload_path']); 230 // Must be directory. 231 if (!is_dir($params['upload_path'])) { 232 App::logMsg(sprintf('Upload directory invalid: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__); 233 trigger_error(sprintf('Upload directory invalid: %s', $params['upload_path']), E_USER_ERROR); 234 } 235 // Must be writable. 236 if (!is_writable($params['upload_path'])) { 237 App::logMsg(sprintf('Upload directory not writable: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__); 238 trigger_error(sprintf('Upload directory not writable: %s', $params['upload_path']), E_USER_ERROR); 239 } 240 // Set the default upload path, stripping any extra slashes if needed. 241 $params['upload_path'] = preg_replace('!/+$!', '', $params['upload_path']); 242 } 243 225 244 // Merge new parameters with old overriding only those passed. 226 245 $this->_params = array_merge($this->_params, $params); … … 250 269 * 251 270 */ 252 function setUploadPath($path)253 {254 $path = realpath($path);255 256 if (!is_dir($path)) {257 App::logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__);258 }259 if (!is_writable($path)) {260 App::logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__);261 }262 263 // Set the default upload path, stripping any extra slashes if needed.264 $this->upload_directory_path = preg_replace('!/+$!', '', $path);265 }266 267 /**268 *269 */270 271 function process($form_name, $custom_file_name=null) 271 272 { 272 273 // Ensure we have a upload directory. 273 if (! isset($this->upload_directory_path)) {274 if (!$this->getParam('upload_path')) { 274 275 App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 275 276 $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); … … 364 365 365 366 // Check to be sure the file has a valid file extension. 366 if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this-> valid_file_extensions)) {367 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this-> valid_file_extensions)), MSG_ERR, __FILE__, __LINE__);367 if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->getParam('valid_file_extensions'))) { 368 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__); 368 369 App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); 369 370 $this->errors[] = $files['name'][$i]; … … 417 418 418 419 // Set the path and file name. 419 $file_path_name = $this-> upload_directory_path. '/' . $file_name;420 $file_path_name = $this->getParam('upload_path') . '/' . $file_name; 420 421 421 422 // Move the file to the final place. 422 423 if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) { 423 chmod($file_path_name, $this-> dest_file_perms);424 chmod($file_path_name, $this->getParam('dest_file_perms')); 424 425 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__); 425 426 if (!isset($custom_file_name) && $files['name'][$i] != $file_name) { … … 449 450 { 450 451 // Ensure we have a upload directory. 451 if (! isset($this->upload_directory_path)) {452 if (!$this->getParam('upload_path')) { 452 453 App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 453 454 return false; 454 455 } 455 456 456 $file_path_name = $this-> upload_directory_path. '/' . $file_name;457 $file_path_name = $this->getParam('upload_path') . '/' . $file_name; 457 458 458 459 if (!is_file($file_path_name)) { … … 474 475 { 475 476 // Ensure we have an upload directory. 476 if (! isset($this->upload_directory_path)) {477 if (!$this->getParam('upload_path')) { 477 478 App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 478 479 return false; 479 480 } 480 481 481 $old_file_path_name = $this-> upload_directory_path. '/' . $old_name;482 $new_file_path_name = $this-> upload_directory_path. '/' . $new_name;482 $old_file_path_name = $this->getParam('upload_path') . '/' . $old_name; 483 $new_file_path_name = $this->getParam('upload_path') . '/' . $new_name; 483 484 if (file_exists($old_file_path_name)) { 484 if (!rename($old_file_path_name, $new_file_path_name)) { 485 if (rename($old_file_path_name, $new_file_path_name)) { 486 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), basename($old_file_path_name), basename($new_file_path_name)), MSG_NOTICE, __FILE__, __LINE__); 487 App::logMsg(sprintf('File renamed from %s to %s', $old_file_path_name, $new_file_path_name), LOG_DEBUG, __FILE__, __LINE__); 488 } else { 485 489 $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__); 486 490 App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__); … … 500 504 { 501 505 // Ensure we have a upload directory. 502 if (! isset($this->upload_directory_path)) {506 if (!$this->getParam('upload_path')) { 503 507 App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); 504 508 return false; 505 509 } 506 510 507 return file_exists($this->upload_directory_path . '/' . $file_name); 511 return file_exists($this->getParam('upload_path') . '/' . $file_name); 512 } 513 514 /** 515 * Get filename by glob pattern. Searches a directory for an image that matches the 516 * specified glob pattern and returns the filename of the first file found. 517 * 518 * @access public 519 * @param string $pattern Pattern to match filename. 520 * @return string filename on success, empty string on failure. 521 * @author Quinn Comendant <quinn@strangecode.com> 522 * @since 15 Nov 2005 20:55:22 523 */ 524 function getFilenameGlob($pattern) 525 { 526 $file_list = glob(sprintf('%s/%s', $this->getParam('upload_path'), $pattern)); 527 if (isset($file_list[0])) { 528 return basename($file_list[0]); 529 } else { 530 return ''; 531 } 508 532 } 509 533
Note: See TracChangeset
for help on using the changeset viewer.