You are here

function drupal_move_uploaded_file in Drupal 7

Same name and namespace in other branches
  1. 8 core/includes/file.inc \drupal_move_uploaded_file()

Moves an uploaded file to a new location.

PHP's move_uploaded_file() does not properly support streams if safe_mode or open_basedir are enabled, so this function fills that gap.

Compatibility: normal paths and stream wrappers.

Parameters

$filename: The filename of the uploaded file.

$uri: A string containing the destination URI of the file.

Return value

TRUE on success, or FALSE on failure.

See also

move_uploaded_file()

http://drupal.org/node/515192

Related topics

1 call to drupal_move_uploaded_file()
file_save_upload in includes/file.inc
Saves a file upload to a new location.

File

includes/file.inc, line 1697
API for handling file uploads and server file management.

Code

function drupal_move_uploaded_file($filename, $uri) {
  $result = @move_uploaded_file($filename, $uri);

  // PHP's move_uploaded_file() does not properly support streams if safe_mode
  // or open_basedir are enabled so if the move failed, try finding a real path
  // and retry the move operation.
  if (!$result) {
    if ($realpath = drupal_realpath($uri)) {
      $result = move_uploaded_file($filename, $realpath);
    }
    else {
      $result = move_uploaded_file($filename, $uri);
    }
  }
  return $result;
}