1

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;
   }
 }
  • Use hook_insert() – developer Dec 2 '16 at 10:01
0

To rename the file, you'll need to use PHP's rename() function. To do so, you'll want to get the file's full local path using drupal_realpath() like so:

// Fetch the local absolute path of our image
$local_path = drupal_realpath($file->uri);

Once you've renamed the file, you'll need to modify the file URI (and potentially other attributes-- use print_r($file) to check).

The wording in the docs is a little weird to me, it says "when a file has been added to the database" which means to me the file metadata has already been saved to the database. However, the example shown in the docs seems to edit this info (but without using &$file ...?) without having to return the file object. Strange.

Anyway, your code might look something like this:

function mymodule_file_presave($file) {
  if ($file->type != 'image') {
    return;
  }
  if (!empty($file->filename)) {

    // Will likely be 'public://'
    $scheme = file_uri_scheme($file->uri);

    // Get full path of image location
    $local_path = drupal_realpath($file->uri);

    // Create new name using drupal scheme format
    $new_uri = $scheme.'new_name_here.jpg';

    // Actual OS rename of file
    rename($local_path, drupal_realpath($new_uri);

    // Update $file object with new uri
    $file->uri = $new_uri;

    // Not sure if this is still needed...
    $file->destination = $file->uri;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.