file_save_data

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
drupal7 file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Save a string to the specified destination and create a database file entry.

see file_unmanaged_save_data()

Übergabeparameter

$data A string containing the contents of the file.

$destination A string containing the destination location. If no value is provided then a randomly name will be generated and the file saved in Drupal's files directory.

$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.

Rückgabewert

A file object, or FALSE on error.

Verwandte Themen

▾ 7 functions call file_save_data()

CronRunTestCase::testTempFileCleanup in modules/system/system.test
Ensure that temporary files are removed.
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.
FileSaveDataTest::testWithFilename in modules/simpletest/tests/file.test
Test the file_save_data() function when a filename is provided.
FileSaveDataTest::testWithoutFilename in modules/simpletest/tests/file.test
Test the file_save_data() function when no filename is provided.
RegistryParseFilesTestCase::setUp in modules/simpletest/tests/registry.test

Code

includes/file.inc, line 1232

<?php
function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  global $user;

  if ($filepath = file_unmanaged_save_data($data, $destination, $replace)) {
    // Create a file object.
    $file = new stdClass();
    $file->fid = NULL;
    $file->filepath = $filepath;
    $file->filename = basename($filepath);
    $file->filemime = file_get_mimetype($file->filepath);
    $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('filepath' => $filepath));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file->fid = $existing->fid;
        $file->filename = $existing->filename;
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
    else if ($replace == FILE_EXISTS_RENAME && is_file(file_create_path($destination))) {
      $file->filename = basename($destination);
    }

    return file_save($file);
  }
  return FALSE;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen