I am using Media Browser widget for uploading and editing images in nodes. My images are stored using image fields. Is it possible to edit URL of the image after uploading? I would need to achieve:
When I upload file named "foo.png" it has by default the following URL:
/sites/default/files/media/.../foo.png
I would need to edit filename to "bar.png", so resulted path will be:
/sites/default/files/media/.../bar.png
(only filename is changed in URL)
I tried to edit filename, since it is possible using Media browser widget. But editing filename does not have affect on change of URL for me. I also tried to use following hook: hook_file_presave I tried to programatically change uri parameter using this hook. But nor filename change neither uri change affected URL of the file. When I look in directory sites/default/files/.. I can see that filename is the same as I uploaded it, not affecting my later changes of filename or uri. When I load php $file object, I can see $file->filename and $file->uri properties changed.
Is it possible to change URL of the file (just last part of the URL - filename) after uploading the file?
//EDIT: I tried to do following changes in my hook_file_presave:
function mymodule_file_presave($file) {
if ($file->type != 'image') {
return;
}
if (!empty($file->filename)) {
$uri_parts = explode('/', $file->uri);
$count = count($uri_parts);
$uri_parts[$count - 1] = $file->filename;
$file->uri = implode('/', $uri_parts);
$file->destination = $file->uri;
}
}