You are here

function videoftp_save_upload in Video 6.5

Same name and namespace in other branches
  1. 6.4 types/videoftp/videoftp_widget.inc \videoftp_save_upload()
1 call to videoftp_save_upload()
videoftp_widget_value in types/videoftp/videoftp_widget.inc
The #value_callback for the videoftp_widget type element.

File

types/videoftp/videoftp_widget.inc, line 320
videoftp widget hooks and callbacks.

Code

function videoftp_save_upload($element) {
  global $user;
  $upload_name = $element['#field_name'] . '_' . $element['#delta'];
  $delta = $element['#delta'];
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $element = $element['#post'][$field['field_name']][$delta];
  $ftp_path = file_directory_path() . '/' . $field['widget']['ftp_path'];
  if (empty($element['ftpselect']) || !file_exists($ftp_path . '/' . $element['ftpselect'])) {
    return 0;
  }
  $video = $element['ftpselect'];
  $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;
  }

  // Begin building the file object.
  $file = new stdClass();
  $file->uid = $user->uid;
  $file->filename = file_munge_filename(trim(basename($video), '.'), $field['widget']['file_extensions']);
  $file->filepath = $ftp_path . '/' . $video;
  $file->filemime = file_get_mimetype($file->filename);
  $file->filesize = filesize($file->filepath);
  $file->status = FILE_STATUS_TEMPORARY;
  $file->timestamp = time();

  // Lets move our file from the ftp folder to the files directory
  $filepath = $file->filepath;
  if (file_move($filepath, $dest)) {

    // Insert new record to the database.
    $file->filepath = $filepath;
    drupal_write_record('files', $file);
  }
  _field_file_cache($file);

  // cache the file in order to minimize load queries
  return $file->fid;
}