Changing file name on upload I'm trying to automatically add prefixes to uploaded file names so they can later be processed by non-drupal scripts. I want to do that while the user is uploading files during creation/editing of a node.

To do that I am using hook_file_validate($field) where I check if the file is correct and then using $field-&gt;destination I define a new file name.

Surprisingly my issue lies in getting context to generate the proper file name (nid of the node,nid of the parent group, or info from other fields). When I upload a file, only part of the website is reloaded by javascript and hook_file_validate is triggered before hook_form_alter . Whene it is triggered I have no access to any data that would tell me where am I.

So far I have tried:

$_REQUEST['destination']


while defined (not a typo) when the whole form is loaded, it is undefined when the file field of the form is reloaded

$_GET['nid']


again - undefined

arg(0); arg(1); arg(2);


returns file ajax field_quest_file
which is not very usefull

I can access uid but it is not enough for me to create unique file names.
Following on from my comment suggesting you do this in an additional submit handler something like this should work:

Add the new submit handler:

function misc_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form_id == 'YOUR_CONTENT_TYPE_node_form') {
    $form['actions']['submit']['#submit'][] = 'rename_files';
  }
}


And then in the new submit handler:

function rename_files($form, &amp;$form_state) {

  $node = $form_state['node'];

  global $user;

  // Loop over the uploaded files. Be sure to re-name field_files to the name of 
  // your file field.
  foreach($node-&gt;field_files[LANGUAGE_NONE] as $item) {

    // Load the file object
    $file = file_load($item['fid']);

    // To set the destination first remove the filename from the stream wrapper 
    // URI.
    $destination = str_replace($file-&gt;filename, '', $file-&gt;uri);

    // Modify this line to the name of your group field.
    $group = $node-&gt;field_group[LANGUAGE_NONE][0]['value'];

    // And then append the group, uid and put the filename back on the end.
    $destination .= $group.$user-&gt;uid.$file-&gt;filename;

    // And finally rename the file.
    file_move($file, $destination);
  }

}