I'm getting the error message

Notice: Undefined index: #field_name in file_managed_file_save_upload() ... line 630 file.module

and could not help myself nor find help on the web.
I defined an image field and tried to upload a default picture. This is being refused. Could be something with the upload directory. Any idea what the problem is?

Thanks in advance,

upunkt

The PHP code is the following:

/**
* Given a managed_file element, save any files that have been uploaded into it.
*
* @param $element
* The FAPI element whose values are being saved.
* @return
* The file object representing the file that was saved, or FALSE if no file
* was saved.
*/

function file_managed_file_save_upload($element) {
  $upload_name = implode('_', $element['#parents']);
  if (empty($_FILES['files']['name'][$upload_name])) {
    return FALSE;
  }

  $destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
  if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
    watchdog('file', '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.', array('%directory' => $destination, '!name' => $element['#field_name']));
    form_set_error($upload_name, t('The file could not be uploaded.'));
    return FALSE;
  }

  if (!$file = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
    watchdog('file', 'The file upload failed. %upload', array('%upload' => $upload_name));
    form_set_error($upload_name, t('The file in the !name field was unable to be uploaded.', array('!name' => $element['#title'])));
    return FALSE;
  }

  return $file;
}

Comments

upunkt’s picture

Status: Active » Closed (works as designed)

Could be solved by setting folder permissions of sites/default/files to owner "nobody" and read/write access. More information to be found here:

http://drupal.org/node/231865#comment-3917728

http://drupal.org/node/202483

rooby’s picture

Component: PHP code » PHP/JS code
Assigned: upunkt » Unassigned
Category: task » bug
Status: Closed (works as designed) » Closed (duplicate)

Just for reference, the error was triggered by the files directory not being writable however there is still an underlying bug in drupal core that is causing the undefined index error.

See #1903010: Notice: Undefined index: #field_name in file_managed_file_save_upload()

rooby’s picture

Issue summary: View changes

Added shown error message to Description

Shashwat Purav’s picture

Hello,

I am getting same error in my site when I try to upload image in custom block module. I ported site from one server to another server. On previous server it was working fine but, now on current server I am getting error. I have checked directory permissions. I have given 755 to files inside default but no luck. Thanking you in anticipation.

Shashwat Purav’s picture

It's working now. The problem was with httpd.conf file. I changed permissions in the file and restart apache and it started working.