How can I change the name of an uploaded file after the node has been saved? I know there is the FileField Paths module for this purpose, but it's still buggy for Drupal 7. I looked at the source of the module, but I can't extract the necessary code.

The only thing I know is that I should use hook_node_update() and field_attach_update(), but I don't know how to use them.

  • So, did you solve your problem?
    – cherouvim
    Commented May 18, 2012 at 10:15

3 Answers 3


Perhaps this will help you. Last night I wrote a code which is meant to auto-generate a file and attach it to the node. When the node is about to be saved, the function removes the uploaded file from the node and creates and attaches the new file.

In your case, you might be able to just update the file name with file_copy and resetting the filefield.

    function MODULE_node_presave($node) {
    *  Out with the old - detach file object

    if (isset($node->field_filefieldname['und'][0]['fid'])) {
    unset($node->field_filefieldname['und'][0]);  //just for good measure

    *  In with the new

    $filename = 'newfilename.dot';
    $filepath = conf_path().'/files/yourpath/';
    $touchout = $node->title;

    $tempFile = file_directory_temp().'/'.$filename;

    file_put_contents($tempFile, $touchout);

    $file = (object) array(
        'uid' => 1,
        'uri' => $tempFile,
        'filemime' => file_get_mimetype($tempFile),
        'status' => 1,
        'display' => 1,

   *  File save operation, puts file in /sites/default/files and records update in db

    if (file_prepare_directory($filepath, FILE_CREATE_DIRECTORY)) {
     $file = file_copy($file, 'public://path/'.$filename, FILE_EXISTS_REPLACE);

   *  Reattaches our severed file, good as new.  

    $node->field_filefieldname['und'][0] = (array)$file;
  • 2
    Did you have any luck?
    – Justin
    Commented Aug 26, 2011 at 7:38

If you know the file id (and don't mind running some PHP code) it's very easy:

$file = file_load($fid);
file_move($file, 'public://new/path');

Here is the method of changing the file when a file has been added to the database just before it get saved by using hook_file_presave():

For example:

 * Implements hook_file_presave().
function hook_file_presave($file) {
  // Change the file timestamp to an hour prior.
  $file->timestamp -= 3600;
  if ($file->filename == 'my_file.wav') {
    $file->filename = 'mynewfile.wav';

See also: How can I rename uploaded files programatically? (auto-rename image files)

Note: This method do not rename filename of file, just filename in file_managed table, so if you'd like to rename the file it-self, you need to move it manually (e.g. $file = file_move($file, $uri);). See this post.

To change it after a file has been added to the database, use hook_file_insert(), for example:

function hook_file_insert($file) {
  // Add a message to the log, if the file is a jpg
  $validate = file_validate_extensions($file, 'jpg');
  if (empty($validate)) {
    watchdog('file', 'A jpg has been added.');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.