You are here

function file_save_data in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/file/file.module \file_save_data()
  2. 4 includes/file.inc \file_save_data()
  3. 5 includes/file.inc \file_save_data()
  4. 6 includes/file.inc \file_save_data()
  5. 9 core/modules/file/file.module \file_save_data()

Saves a file to the specified destination and creates a database entry.

Parameters

$data: A string containing the contents of the file.

$destination: A string containing the destination URI. This must be a stream wrapper URI. If no value is provided, a randomized name will be generated and the file will be saved using Drupal's default files scheme, usually "public://".

$replace: Replace behavior when the destination file already exists:

  • FILE_EXISTS_REPLACE - Replace the existing file. If a managed file with the destination name exists then its database entry will be updated. If no database entry is found then a new one will be created.
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.

Return value

A file object, or FALSE on error.

See also

file_unmanaged_save_data()

Related topics

9 calls to file_save_data()
CronRunTestCase::testTempFileCleanup in modules/system/system.test
Ensure that temporary files are removed.
FileFieldTestCase::createTemporaryFile in modules/file/tests/file.test
Creates a temporary file, for a specific user.
FileSaveDataTest::testExistingError in modules/simpletest/tests/file.test
Test that file_save_data() fails overwriting an existing file.
FileSaveDataTest::testExistingRename in modules/simpletest/tests/file.test
Test file_save_data() when renaming around an existing file.
FileSaveDataTest::testExistingReplace in modules/simpletest/tests/file.test
Test file_save_data() when replacing an existing file.

... See full list

File

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

Code

function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  global $user;
  if (empty($destination)) {
    $destination = file_default_scheme() . '://';
  }
  if (!file_valid_uri($destination)) {
    watchdog('file', 'The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', array(
      '%destination' => $destination,
    ));
    drupal_set_message(t('The data could not be saved, because the destination is invalid. More information is available in the system log.'), 'error');
    return FALSE;
  }
  if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {

    // Create a file object.
    $file = new stdClass();
    $file->fid = NULL;
    $file->uri = $uri;
    $file->filename = drupal_basename($uri);
    $file->filemime = file_get_mimetype($file->uri);
    $file->uid = $user->uid;
    $file->status = FILE_STATUS_PERMANENT;

    // If we are replacing an existing file re-use its database record.
    if ($replace == FILE_EXISTS_REPLACE) {
      $existing_files = file_load_multiple(array(), array(
        'uri' => $uri,
      ));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file->fid = $existing->fid;
        $file->filename = $existing->filename;
      }
    }
    elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
      $file->filename = drupal_basename($destination);
    }
    return file_save($file);
  }
  return FALSE;
}