public function S3fsFileService::saveData in S3 File System 8.3
Same name and namespace in other branches
- 4.0.x src/S3fsFileService.php \Drupal\s3fs\S3fsFileService::saveData()
Saves a file to the specified destination without invoking file API.
This function is identical to file_save_data() except the file will not be saved to the {file_managed} table and none of the file_* hooks will be called.
Parameters
string $data: A string containing the contents of the file.
string $destination: A string containing the destination location. This must be a stream wrapper URI.
int $replace: Replace behavior when the destination file already exists:
- FileSystemInterface::EXISTS_REPLACE - Replace the existing file.
- FileSystemInterface::EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
- FileSystemInterface::EXISTS_ERROR - Do nothing and return FALSE.
Return value
string A string with the path of the resulting file, or FALSE on error.
Throws
\Drupal\Core\File\Exception\FileException Implementation may throw FileException or its subtype on failure.
Overrides FileSystemInterface::saveData
See also
File
- src/
S3fsFileService.php, line 399
Class
- S3fsFileService
- Provides helpers to operate on files and stream wrappers.
Namespace
Drupal\s3fsCode
public function saveData($data, $destination, $replace = self::EXISTS_RENAME) {
// Write the data to a temporary file.
$temp_name = $this
->tempnam('temporary://', 'file');
if (file_put_contents($temp_name, $data) === FALSE) {
$this->logger
->error("Temporary file '%temp_name' could not be created.", [
'%temp_name' => $temp_name,
]);
throw new FileWriteException("Temporary file '{$temp_name}' could not be created.");
}
// Move the file to its final destination.
return $this
->move($temp_name, $destination, $replace);
}