Renaming files with a random name.

Submitted by victor.bourgade on Thu, 11/05/2020 - 18:14

Sometimes for security reasons or for randomizing visitors input data on your server you might want to randomize file names.

The best way to do this is to take the file entity at the very beginning of its creation using hook_entity_create. At this specific time, the entity object holds the filename uploaded by the user and the temporary URI (/tmp/XXXXX.ext) of the file on your server. So changing the filename at this stage will let Drupal do the rest for you, moving the file from the /tmp folder to the Drupal file folder with the updated random name.

To create a random name we can use the Drupal utility Random::name();

If by any chance we get a non-unique name, the entity creation process will still rename the file as XXXX_1.ext as it usually does. Also, you can use the unique second parameter of Random::name(int, unique = TRUE) if you really want unique file names.

You can try this snippet, it'll do the job for you:

 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Component\Utility\Random;
  
/**
 * Implements hook_ENTITY_TYPE_create().
 */
function HOOK_file_create(EntityInterface $entity) {
  // Rename files with a random name.
  $filename = $entity->get('filename')->value;
  $file_ext = end(explode('.', $filename));
  $random = new Random;
  $new_filename = $random->name(12) . '.' . $file_ext; 
  $entity->set('filename', $new_filename);
}

In case you really want/need to hardcode the uri with the new filename you can simply set it as followed (but previous snippet should be enough):

/**
 * Implements hook_ENTITY_TYPE_create().
 */
function HOOK_file_create(EntityInterface $entity) {
  // Rename files with a random name.
  $filename = $entity->get('filename')->value;
  $file_parts = explode('.', $filename);
  $file_ext = end($file_parts);
  $random = new Random;
  $new_filename = $random->name(12) . '.' . $file_ext;
  $uri = $entity->get('uri')->first()->getValue()['value'];
  if (strpos($uri, $new_filename) == FALSE) {
    $parts = explode('/', $uri);
    $last_key = array_key_last($parts);
    $parts[$last_key] = $new_filename;
    $new_uri = implode('/', $parts);
    $entity->set('uri', $new_uri);
  }
  $entity->set('filename', $new_filename);
}

 

About the writer

victor.bourgade

Victor is a web developer passionnated in drupal and bootstrap technologies. He likes challenges and beautiful designs.

When not behind his computer you'll find him drinking beers with friends or in the middle of nowhere hiking with his dog.