8.5.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.0.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.1.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.2.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.3.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.4.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8.6.x file.module file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
4.6.x file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
4.7.x file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
5.x file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
6.x file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
7.x file.inc file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Save a string to the specified destination.


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

$dest A string containing the destination location.:

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

Return value

A string containing the resulting filename or 0 on error

Related topics

3 calls to file_save_data()
drupal_build_css_cache in includes/common.inc
Aggregate and optimize CSS files, putting them in the files directory.
drupal_build_js_cache in includes/common.inc
Aggregate JS files, putting them in the files directory.
_color_save_stylesheet in modules/color/color.module
Save the rewritten stylesheet to disk.


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


function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) {
  $temp = file_directory_temp();

  // On Windows, tempnam() requires an absolute path, so we use realpath().
  $file = tempnam(realpath($temp), 'file');
  if (!($fp = fopen($file, 'wb'))) {
    drupal_set_message(t('The file could not be created.'), 'error');
    return 0;
  fwrite($fp, $data);
  if (!file_move($file, $dest, $replace)) {
    return 0;
  return $file;


p0lar_bear’s picture

I've found one way to inspect the structure of an array that isn't detailed in documentation is to have it dumped to a file via output buffering and using file_save_data. A similar approach could be used for pretty much any other PHP-based platform, though how you save the file will differ from platform to platform.

Just stick this anywhere after the array you want to check is populated...

$array_out = ob_get_contents();
file_save_data($array_out, 'array.txt', FILE_EXISTS_REPLACE);

...and the array dump will be in your site's filesystem as array.txt (e.g. sites/default/files/array.txt).

a6hiji7’s picture

That may have some use cases. Normally, you can simply dump variables as watchdog log entries by simply doing something like the following -

watchdog('Some identifier', 'Dumping variable - !var', array('!var' => print_r($var, 1)), WATCHDOG_NOTICE);

giorgio79’s picture

Much better

Just output the array with dpm

kle’s picture

is not the folder - it must be the filepath.
Here a snippet to load a remote-Image from $fileURL into Drupal-Temp-Folder:

  $filename = basename($fileURL);
  $request = drupal_http_request($fileURL); // load file into RAM
  if ($image = $request->data) {
    $filepath = file_directory_temp().'/'.$filename;
    file_save_data($image, $filepath);