I would like to rename the files uploaded to a webform according to some tokens or the value in some other field.

I did some research on this and was surprised to find no definite solution.

For example:

  • I'm 1 of the approved answers in your post. I understand your concern. I recommend making a small module or attempt to provide this new functionality in a patch to close existing issue(s) for webform. You should realize webform was born very separate from drupal -- hence all these types of integration issues that when using other modules, fields or Views we never face. If it was super easy -- it'd already be done. Webform is a big module with alot of legacy functionality.
    – tenken
    Oct 10, 2014 at 20:11
  • Yes, tenken. You have certainly provided a good answer on that post but I wish there was a solution somewhere. Anyways, I will hook into webform and post my solution here. One final solution.
    – shivams
    Oct 11, 2014 at 2:48

2 Answers 2


The sandbox project is removed as the issue queue get continued, the latest patch has been failed in simpletest, but you can still check this patch


You can change every file upload by hook_file_presave as your desire pattern as example

function yourmodulename_file_presave($file) {
  $parts = pathinfo($file->filename);
  $file->filename = 'mypattern_'.$file->uid .'_'. $file->timestamp . '.' . $parts['extension'];

but this method do not rename filname(it just rename file name in file_managed table), if you want also rename file name (uri of file) you should use below code

function yourmodulename_file_insert($file) {
  $parts = pathinfo($file->filename);
  $uri = 'public://'.'mypattern_'.$file->uid .'_'. $file->timestamp . '.' . $parts['extension'];
  $file=file_move($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.