Rename files during upload drupal

When is it needed?
Users sometimes give strange names for their files, if you do not want to have links to the files with names like they given use this snippet in your module. 🙂
The result is renamed file (something like this 8ca0c8e9d2d9bdccec5cbc1e0760554c.jpg).

file-upload

/**
 * Implements hook_file_insert().
 */
function MYMODULE_file_insert($file) {
  $hash = 'public://' . md5($file->filename) . '.' . pathinfo($file->filename, PATHINFO_EXTENSION);
   file_move($file, $hash, 'FILE_EXIST_REPLACE');
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s