file_save_upload

Versions
4.6
file_save_upload($source, $dest = 0, $replace = FILE_EXISTS_RENAME)
4.7
file_save_upload($source, $dest = false, $replace = FILE_EXISTS_RENAME)
5
file_save_upload($source, $dest = FALSE, $replace = FILE_EXISTS_RENAME)
6
file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME)
7
file_save_upload($source, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME)

Saves a file upload to a new location. The source file is validated as a proper upload and handled as such.

Parameters

$source A string specifying the name of the upload field to save. This parameter will contain the resulting destination filename in case of success.

$dest A string containing the directory $source should be copied to, will use the temporary directory in case no other value is set.

$replace A boolean, set to true if the destination should be replaced when in use, but when false append a _X to the filename.

Return value

An object containing file info or 0 in case of error.

Related topics

▾ 7 functions call file_save_upload()

fileupload_insert in developer/examples/fileupload.module
fileupload_update in developer/examples/fileupload.module
fileupload_validate in developer/examples/fileupload.module
system_theme_settings in modules/system.module
Menu callback; display theme configuration for entire site and individual themes.
upload_nodeapi in modules/upload.module
Implementation of hook_nodeapi().
upload_save in modules/upload.module
user_validate_picture in modules/user.module

Code

includes/file.inc, line 382

<?php
function file_save_upload($source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
  // Make sure $source exists in $_FILES.
  if ($file = file_check_upload($source)) {
    if (!$dest) {
      $dest = variable_get('file_directory_temp', FILE_DIRECTORY_TEMP);
      $temporary = 1;
      if (is_file($file->filepath)) {
        // If this file was uploaded by this user before replace the temporary copy.
        $replace = 1;
      }
    }

    // Check for file upload errors.
    switch ($file->error) {
      case 0: // UPLOAD_ERR_OK
        break;
      case 1: // UPLOAD_ERR_INI_SIZE
      case 2: // UPLOAD_ERR_FORM_SIZE
        drupal_set_message(t('File upload failed: file size too big.'), 'error');
        return 0;
      case 3: // UPLOAD_ERR_PARTIAL
      case 4: // UPLOAD_ERR_NO_FILE
        drupal_set_message(t('File upload failed: incomplete upload.'), 'error');
        return 0;
      default: // Unknown error
        drupal_set_message(t('File upload failed: unknown error.'), 'error');
        return 0;
    }

    unset($_SESSION['file_uploads'][is_object($source) ? $source->source : $source]);
    if (file_move($file, $dest, $replace)) {
      if ($temporary) {
        $_SESSION['file_uploads'][is_object($source) ? $source->source : $source] = $file;
      }
      return $file;
    }
    return 0;
  }
  return 0;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.