Rename files during upload.


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 f*ckyou.jpg use this snippet in your module. :)
The result is renamed file (something like this 8ca0c8e9d2d9bdccec5cbc1e0760554c.jpg).

Get raw version
  1. /**
  2.  * Implements hook_file_insert().
  3.  */
  4. function MYMODULE_file_insert($file) {
  5. $hash = 'public://' . md5($file->filename) . '.' . pathinfo($file->filename, PATHINFO_EXTENSION);
  6. file_move($file, $hash, 'FILE_EXIST_REPLACE');
  7. }


Ruslan's picture

Isn't work if need use custom folders for files.