Notice: Undefined index: #field_name in file_managed_file_save_upload()

Created on 30 January 2013, about 12 years ago
Updated 24 February 2025, 12 days ago

When I add a FAPI managed file field to a non-entity form and upload a file when the sites/default/files directory has insufficient permissions to write I get the following message:

Notice: Undefined index: #field_name in file_managed_file_save_upload() (line 638 of /var/www/ae7/modules/file/file.module).

The problem being that on non-entity forms #field_name is not set.

This affects things like the media module, along with these other issues:
#1373046: Zen | Notice: Undefined index: #field_name in file_managed_file_save_upload() | line 630 file.module
#1218048: Notice: Undefined index: #field_name in file_managed_file_save_upload()

So either the problem is that non-entity form elements should be getting a #field_name, or other modules should not be relying on #field_name being there.

It seems like it should be the former.

I still need to check drupal 8 for the same issue.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

file.module

Created by

🇦🇺Australia rooby

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.

  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

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.

  • 🇦🇺Australia imclean Tasmania

    #76:

    +++ b/core/modules/file/file.module
    @@ -1435,7 +1435,7 @@ function file_managed_file_save_upload($element, FormStateInterface $form_state)
    -    \Drupal::logger('file')->notice('The upload directory %directory for the file field %name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', ['%directory' => $destination, '%name' => $element['#field_name']]);
    +    \Drupal::logger('file')->notice('TThe upload directory %directory for the %name field could not be created or is not accessible, so the uploaded file was not saved.', ['%directory' => $destination, '%name' => $element['#field_name']]);
    

    From the issue summary:

    The problem being that on non-entity forms #field_name is not set

    This issue is about removing the reliance on $element['#field_name'] being set.

    Another way this problem shows itself is if the %name parameter is NULL when file.module logs an error, then the dblog page displays a white screen with the text "The website encountered an unexpected error. Try again later." The only way to fix this is to delete the entry in the watchdog table.

Production build 0.71.5 2024