You are here

function _filefield_file_upload in FileField 6.2

Upload a file to the given field and delta (or try to, at least), and update the corresponding part of the form state with the new file data.

2 calls to _filefield_file_upload()
filefield_file_upload_js in ./filefield.widget.inc
Form callback for the "Upload" button with JavaScript enabled, invoked by filefield_js(). Uploads a file to the given field and delta.
filefield_file_upload_submit in ./filefield.widget.inc
Submit callback for the "Upload" button next to each file upload field.

File

./filefield.widget.inc, line 190
FileField: Defines a CCK file field type.

Code

function _filefield_file_upload(&$form_state, $field, $delta) {
  $field_name = $field['field_name'];
  if (module_exists('token')) {
    global $user;
    $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user);
  }
  else {
    $widget_file_path = $field['widget']['file_path'];
  }

  // Let modules provide their own validators.
  $validators = _filefield_upload_validators($field, $field['widget'], $form_state['values'][$field_name]);
  $upload_name = $field_name . '_' . $delta;
  $complete_file_path = file_directory_path() . '/' . $widget_file_path;
  $file =& $form_state['values'][$field_name][$delta];
  $replaced_file = $file['replaced_file'];
  if (!filefield_check_directory($widget_file_path, $upload_name)) {
    watchdog('file', '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' => $widget_file_path,
      '%field' => $field_name,
      '%type' => $field['type_name'],
    ));
    $file = array(
      'fid' => 0,
      'replaced_file' => $replaced_file,
    );
    return $file;
  }
  if (!($file = field_file_save_upload($upload_name, $validators, $complete_file_path))) {
    watchdog('file', 'The file %file could not be saved as addition to the file field %field (content type %type). This can be a consequence of the file failing validation, or if it can\'t be moved to the file directory, or whatever reason the file framework comes up with. No further information is available to the filefield module, but if you\'re lucky then that function left one or more hints in the log as well (directly before this log entry).', array(
      '%file' => $complete_file_path,
      '%field' => $field_name,
      '%type' => $field['type_name'],
    ));
    $file = array(
      'fid' => 0,
      'replaced_file' => $replaced_file,
    );
    return $file;
  }
  $file_default_properties = array(
    'list' => 1,
    'description' => $file['filename'],
  );
  $file = array_merge($file_default_properties, $file);
  $file['replaced_file'] = $replaced_file;
  return $file;
}