function file_save_data in Drupal 4
Same name and namespace in other branches
- 8 core/modules/file/file.module \file_save_data()
- 5 includes/file.inc \file_save_data()
- 6 includes/file.inc \file_save_data()
- 7 includes/file.inc \file_save_data()
- 9 core/modules/file/file.module \file_save_data()
Save a string to the specified destination.
Parameters
$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.:
- FILE_EXISTS_REPLACE - Replace the existing file
- FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
- FILE_EXISTS_ERROR - Do nothing and return false.
Return value
A string containing the resulting filename or 0 on error
Related topics
1 call to file_save_data()
- blogapi_metaweblog_new_media_object in modules/
blogapi.module - Blogging API callback. Inserts a file into Drupal.
File
- includes/
file.inc, line 523 - API for handling file uploads and server file management.
Code
function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) {
$temp = file_directory_temp();
$file = tempnam($temp, 'file');
if (!($fp = fopen($file, 'wb'))) {
drupal_set_message(t('The file could not be created.'), 'error');
return 0;
}
fwrite($fp, $data);
fclose($fp);
if (!file_move($file, $dest, $replace)) {
return 0;
}
return $file;
}