Changeset 22 for trunk/lib/Upload.inc.php
- Timestamp:
- Nov 23, 2005 9:29:33 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Upload.inc.php
r21 r22 30 30 'dest_file_perms' => 0600, 31 31 32 // Require file to have one of the following file extentions.32 // Require file to have one of the following file name extentions. 33 33 'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm'), 34 34 ); … … 37 37 var $errors = array(); 38 38 39 // Array of file extensions and corresponding mime-types.39 // Array of file name extensions and corresponding mime-types. 40 40 var $mime_extension_map = array( 41 41 'Z' => 'application/x-compress', … … 278 278 } 279 279 280 // Ensure the file form element specified actually exists. 280 281 if (!isset($_FILES[$form_name])) { 281 App::logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__);282 $this->raiseMsg(_("There was a problem with the file upload. Please try again ."), MSG_ERR, __FILE__, __LINE__);282 App::logMsg(sprintf(_("Form element %s does not exist."), $form_name), LOG_ERR, __FILE__, __LINE__); 283 $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); 283 284 return false; 284 285 } … … 314 315 } 315 316 316 // Check The php upload error messages. 317 if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) { 318 if ($this->getParam('display_messages')) { 319 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $files['name'][$i], ini_get('upload_max_filesize')), MSG_ERR, __FILE__, __LINE__); 320 } 321 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_INI_SIZE (currently %s)."), $files['error'][$i], $files['name'][$i], ini_get('upload_max_filesize')), LOG_ERR, __FILE__, __LINE__); 322 $this->errors[] = $files['name'][$i]; 323 continue; 324 } 325 if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) { 326 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $files['name'][$i], $_POST['MAX_FILE_SIZE']), MSG_ERR, __FILE__, __LINE__); 327 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_FORM_SIZE (currently %s)."), $files['error'][$i], $files['name'][$i], $_POST['MAX_FILE_SIZE']), LOG_ERR, __FILE__, __LINE__); 328 $this->errors[] = $files['name'][$i]; 329 continue; 330 } 331 if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) { 332 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 333 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); 334 $this->errors[] = $files['name'][$i]; 335 continue; 336 } 337 if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) { 338 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 339 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_FILE."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); 340 $this->errors[] = $files['name'][$i]; 341 continue; 342 } 343 if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) { 344 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 345 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_TMP_DIR."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); 346 $this->errors[] = $files['name'][$i]; 347 continue; 348 } 349 350 // Check to be sure it's an uploaded file. 351 if (!is_uploaded_file($files['tmp_name'][$i])) { 352 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 353 App::logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); 354 $this->errors[] = $files['name'][$i]; 355 continue; 356 } 357 358 // Check to be sure the file is not empty. 359 if ($files['size'][$i] < 1) { 360 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 361 App::logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); 362 $this->errors[] = $files['name'][$i]; 363 continue; 364 } 365 366 // Check to be sure the file has a valid file extension. 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__); 369 App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); 370 $this->errors[] = $files['name'][$i]; 371 continue; 372 } 373 374 // Check to be sure the file has a unique file name. 375 if (!$this->getParam('allow_overwriting') && $this->exists($files['name'][$i])) { 376 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); 377 App::logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); 378 $this->errors[] = $files['name'][$i]; 379 continue; 380 } 381 382 // Determine file name. 317 // Determine final file name. 383 318 if ($num == 1) { 384 319 // Single upload. … … 420 355 $file_path_name = $this->getParam('upload_path') . '/' . $file_name; 421 356 357 358 // Check The php upload error messages. 359 if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) { 360 if ($this->getParam('display_messages')) { 361 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $file_name, ini_get('upload_max_filesize')), MSG_ERR, __FILE__, __LINE__); 362 } 363 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_INI_SIZE (currently %s)."), $files['error'][$i], $file_name, ini_get('upload_max_filesize')), LOG_ERR, __FILE__, __LINE__); 364 $this->errors[] = $file_name; 365 continue; 366 } 367 if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) { 368 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $file_name, $_POST['MAX_FILE_SIZE']), MSG_ERR, __FILE__, __LINE__); 369 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_FORM_SIZE (currently %s)."), $files['error'][$i], $file_name, $_POST['MAX_FILE_SIZE']), LOG_ERR, __FILE__, __LINE__); 370 $this->errors[] = $file_name; 371 continue; 372 } 373 if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) { 374 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $file_name), MSG_ERR, __FILE__, __LINE__); 375 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $file_name), LOG_ERR, __FILE__, __LINE__); 376 $this->errors[] = $file_name; 377 continue; 378 } 379 if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) { 380 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $file_name), MSG_ERR, __FILE__, __LINE__); 381 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_FILE."), $files['error'][$i], $file_name), LOG_ERR, __FILE__, __LINE__); 382 $this->errors[] = $file_name; 383 continue; 384 } 385 if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) { 386 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $file_name), MSG_ERR, __FILE__, __LINE__); 387 App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_TMP_DIR."), $files['error'][$i], $file_name), LOG_ERR, __FILE__, __LINE__); 388 $this->errors[] = $file_name; 389 continue; 390 } 391 392 // Check to be sure it's an uploaded file. 393 if (!is_uploaded_file($files['tmp_name'][$i])) { 394 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $file_name), MSG_ERR, __FILE__, __LINE__); 395 App::logMsg(sprintf(_("The file %s failed is_uploaded_file."), $file_name), LOG_ERR, __FILE__, __LINE__); 396 $this->errors[] = $file_name; 397 continue; 398 } 399 400 // Check to be sure the file is not empty. 401 if ($files['size'][$i] < 1) { 402 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); 403 App::logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $file_name), LOG_ERR, __FILE__, __LINE__); 404 $this->errors[] = $file_name; 405 continue; 406 } 407 408 // Check to be sure the file has a valid file name extension. 409 if (!in_array(strtolower($this->getFilenameExtension($file_name)), $this->getParam('valid_file_extensions'))) { 410 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file name extensions: %s."), $file_name, join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__); 411 App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file name extension."), $file_name), LOG_WARNING, __FILE__, __LINE__); 412 $this->errors[] = $file_name; 413 continue; 414 } 415 416 // Check to be sure the file has a unique file name. 417 if (!$this->getParam('allow_overwriting') && $this->exists($file_name)) { 418 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $file_name), MSG_ERR, __FILE__, __LINE__); 419 App::logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $file_name), LOG_WARNING, __FILE__, __LINE__); 420 $this->errors[] = $file_name; 421 continue; 422 } 423 422 424 // Move the file to the final place. 423 425 if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) { 424 426 chmod($file_path_name, $this->getParam('dest_file_perms')); 425 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $file s['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__);427 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $file_name), MSG_SUCCESS, __FILE__, __LINE__); 426 428 if (!isset($custom_file_name) && $files['name'][$i] != $file_name) { 427 429 // Notify user if uploaded file name was modified (unless a custom file name will be used anyways). … … 435 437 continue; 436 438 } else { 437 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $file s['name'][$i]), MSG_ERR, __FILE__, __LINE__);439 $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $file_name), MSG_ERR, __FILE__, __LINE__); 438 440 App::logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__); 439 $this->errors[] = $files['name'][$i]; 440 continue; 441 } 442 } 443 return (sizeof($new_file_names) > 0) ? $new_file_names : false; 441 $this->errors[] = $file_name; 442 continue; 443 } 444 } 445 446 // Return names of files uploaded (or empty array when none processed). 447 return $new_file_names; 444 448 } 445 449 … … 543 547 * 544 548 */ 549 function anyErrors() 550 { 551 return sizeof($this->errors) > 0; 552 } 553 554 /** 555 * 556 */ 545 557 function cleanFileName($file_name) 546 558 { … … 560 572 { 561 573 preg_match('/.*?\.(\w+)$/i', $file_name, $ext); 562 return $ext[1];574 return isset($ext[1]) ? $ext[1] : ''; 563 575 } 564 576
Note: See TracChangeset
for help on using the changeset viewer.