You are here

function filefield_save_upload in FileField 6.3

Given a FAPI element, save any files that may have been uploaded into it.

This function should only be called during validate, submit, or value_callback functions.

Parameters

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

1 call to filefield_save_upload()
filefield_widget_value in ./filefield_widget.inc
The #value_callback for the filefield_widget type element.

File

./filefield_widget.inc, line 187
This file contains CCK widget related functionality.

Code

function filefield_save_upload($element) {
  $upload_name = implode('_', $element['#array_parents']);
  $field = content_fields($element['#field_name'], $element['#type_name']);
  if (empty($_FILES['files']['name'][$upload_name])) {
    return 0;
  }
  $dest = filefield_widget_file_path($field);
  if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
    watchdog('filefield', 'The upload directory %directory for the file field %field (content type %type) 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' => $dest,
      '%field' => $element['#field_name'],
      '%type' => $element['#type_name'],
    ));
    form_set_error($upload_name, t('The file could not be uploaded.'));
    return 0;
  }
  if (!($file = field_file_save_upload($upload_name, $element['#upload_validators'], $dest))) {
    watchdog('filefield', 'The file upload failed. %upload', array(
      '%upload' => $upload_name,
    ));
    form_set_error($upload_name, t('The file in the @field field was unable to be uploaded.', array(
      '@field' => $element['#title'],
    )));
    return 0;
  }
  return $file['fid'];
}