file_save_data

5 file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
6 file.inc file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME)
7 file.inc file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
8 file.inc file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Save a string to the specified destination

Parameters

$data A string containing the contents of the file:

$dest A string containing the destination location:

Return value

A string containing the resulting filename or 0 on error

Related topics

1 call to file_save_data()

File

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

Code

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

  $temp = variable_get('file_directory_temp', FILE_DIRECTORY_TEMP);
  $file = tempnam($temp, 'file');
  if (!$fp = fopen($file, 'wb')) {
    drupal_set_message(t('Unable to create file.'), 'error');
    return 0;
  }
  fwrite($fp, $data);
  fclose($fp);

  if (!file_move($file, $dest, $replace)) {
    return 0;
  }

  return $file;
}
Login or register to post comments