You are here

function file_managed_file_save_upload in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/file/file.module \file_managed_file_save_upload()
  2. 9 core/modules/file/file.module \file_managed_file_save_upload()

Saves any files that have been uploaded into a managed_file element.

Parameters

$element: The FAPI element whose values are being saved.

Return value

The file object representing the file that was saved, or FALSE if no file was saved.

1 call to file_managed_file_save_upload()
file_managed_file_value in modules/file/file.module
The #value_callback for a managed_file type element.

File

modules/file/file.module, line 686
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

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;
}