Display generic message and log detailed message when file upload fails due to PHP error

Created on 4 November 2006, over 17 years ago
Updated 25 March 2024, 3 months ago

Problem/Motivation

When PHP returns an error during a file upload attempt (see http://php.net/manual/features.file-upload.errors.php), an error message with details potentially about the server configuration are displayed to the user. These file upload errors should be logged instead and a generic message displayed to the user.

Original problem/motivation:

When PHP throws UPLOAD_ERR_NO_TMP_DIR error while uploading a file, function won't handle it correctly. in fact this function will quit leaving user with cryptic error message An unknown error has occurred. The error is known, however no one bothered to write separate handler for it.

Steps to reproduce

Proposed resolution

1. Check for the following errors:

  • UPLOAD_ERR_FORM_SIZE: "The file $original_file_name could not be saved because it exceeds " . format_size(Environment::getUploadMaxSize()) . ", the maximum allowed size for uploads.";
  • UPLOAD_ERR_NO_FILE: "The file $original_file_name could not be saved because the upload did not complete.";
  • UPLOAD_ERR_NO_TMP_DIR: "The file $original_file_name could not be saved because the server is missing a temporary folder.";
  • UPLOAD_ERR_CANT_WRITE: "The file $original_file_name could not be saved because the server is unable to write to disk.";
  • UPLOAD_ERR_EXTENSION: "The file $original_file_name could not be saved because a PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.";

2. Present an unconditional user-facing error that doesn't divulge any specifics that might divulge information about the server and be a security risk: \Drupal::messenger()->addError(t('The file %file could not be saved. A server error has occurred.', ['%file' => $original_file_name]));

Remaining tasks



Review
Commit

User interface changes

API changes

Data model changes

Release notes snippet

Beta phase evaluation

<!--Uncomment the relevant rows for the issue. -->
πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
File systemΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡΅πŸ‡±Poland SiliconMind

Live updates comments and jobs are added and updated live.
  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

  • Needs screenshots

    The change alters the user interface, so before and after screenshots should be added to document the UI change. Make sure to capture the relevant region only. Use a tool such as Aviary on Windows or Skitch on Mac OS X.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024