You are here

static function ResourceUpload::save_upload in D7 Media 6

Saves a file upload to a temporary location

The file will be added to the files table as a temporary file. Temporary files are periodically cleaned. To make the file permanent file call it's set_permanent() method.

Parameters

$source: A string specifying the name of the upload field to save.

$validators: An optional, associative array of callback functions used to validate the file. The keys are function names and the values arrays of callback parameters which will be passed in after the user and file objects. The functions should return an array of error messages, an empty array indicates that the file passed validation. The functions will be called in the order specified.

$destination: A string containing the directory $source should be copied to. If this is not provided or is not writable, the temporary directory will be used.

$replace: A boolean indicating whether an existing file of the same name in the destination directory should overwritten. A FALSE value will generate a new, unique filename in the destination directory.

Return value

An object containing the file information, or FALSE in the event of an error.

1 call to ResourceUpload::save_upload()
ResourceUpload::load in resource/resource.module

File

resource/resource.module, line 399
Resource API for Drupal, a replacement for files.

Class

ResourceUpload

Code

static function save_upload($source) {

  // check and see if there were any errors.
  if (drupal_file_upload::upload_error($source)) {
    return FALSE;
  }

  // Begin building file object.
  $file = new stdClass();
  $file->source = $source;
  $file->uid = $user->uid;
  $file->filename = basename($_FILES['files']['name'][$source]);

  // create a tmp path to use until the file is save to a final location else where.
  // we just use a random string to defang the file for processing in tmp.
  $file->filepath = file_destination(uniqid(), file_directory_temp(), FALSE);
  $file->filemime = $_FILES['files']['type'][$source];
  $file->filesize = $_FILES['files']['size'][$source];

  // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
  // directory. This overcomes open_basedir restrictions for future file
  // operations.
  if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath)) {
    form_set_error($source, t('File upload error. Could not move uploaded file.'));
    watchdog('file api', 'Upload error. Could not move uploaded file %file to destination %destination.', array(
      '%file' => $file->filename,
      '%destination' => $file->filepath,
    ));
    return FALSE;
  }
  return $file;
}