function _file_resource_create in Services 7.3
Same name and namespace in other branches
- 6.3 resources/file_resource.inc \_file_resource_create()
Adds a new file and returns the fid.
Parameters
$file: An array as representing the file with a base64 encoded $file['file']
Return value
Unique identifier for the file (fid) or errors if there was a problem.
1 string reference to '_file_resource_create'
- _file_resource_definition in resources/
file_resource.inc - THERE SHOULD BE NO UPDATE!!! Drupal doesn't allow updating or replacing a file in the files table. If you need to, create a new file and remove the old file.
File
- resources/
file_resource.inc, line 161 - File resource.
Code
function _file_resource_create($file) {
// Adds backwards compatability with regression fixed in #1083242
// $file['file'] can be base64 encoded file so we check whether it is
// file array or file data.
$file = _services_arg_value($file, 'file');
// If the file data or filename is empty then bail.
if (!isset($file['file']) || empty($file['filename'])) {
return services_error(t("Missing data the file upload can not be completed"), 500);
}
// Sanitize the file extension, name, path and scheme provided by the user.
$destination = empty($file['filepath']) ? file_default_scheme() . '://' . _services_file_check_destination($file['filename']) : _services_file_check_destination_uri($file['filepath']);
$dir = drupal_dirname($destination);
// Build the destination folder tree if it doesn't already exists.
if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
return services_error(t("Could not create destination directory for file."), 500);
}
// Write the file
if (!($file_saved = file_save_data(base64_decode($file['file']), $destination))) {
return services_error(t("Could not write file to destination"), 500);
}
if (isset($file['status']) && $file['status'] == 0) {
// Save as temporary file.
$file_saved->status = 0;
file_save($file_saved);
}
else {
// Required to be able to reference this file.
file_usage_add($file_saved, 'services', 'files', $file_saved->fid);
}
return array(
'fid' => $file_saved->fid,
'uri' => services_resource_uri(array(
'file',
$file_saved->fid,
)),
);
}