Can anyone suggest a way to change the name of a file uploaded via CCK/filefield based on a user-submitted name? Clearly it would be simplest if they just changed the name before uploading the file, but for whatever reason I have a client that is insisting on this capability. Ideally I'd like another field in my custom content type with the new filename, which would be applied on uploading.

Comments

phl3tch’s picture

nudge...?

arvind’s picture

hello

were you able to find a solution for this? i have also been looking for a similair feature for my website.

i agree that a field in the settings should possibly help solve this issue.

any other ideas?

arvi

arvind’s picture

i changed the insert value for the file table from $file[filename] to $file['description']

function filefield_file_insert($node, &$file, $field) {
  $fieldname = $field['field_name'];
  if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
    $file = (array)$file;
    $file['fid'] = db_next_id('{files}_fid');
    db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
             VALUES (%d, %d, "%s","%s","%s",%d)',
    // modified to insert filename $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
            $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
    return (array)$file;
  }
  else {
    // Include file name in upload error.
    form_set_error(NULL, t('file upload was unsuccessful.'));
    return FALSE;
  }
}

to

function filefield_file_insert($node, &$file, $field) {
  $fieldname = $field['field_name'];
  if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
    $file = (array)$file;
    $file['fid'] = db_next_id('{files}_fid');
    db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
             VALUES (%d, %d, "%s","%s","%s",%d)',
    // modified to insert filename $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
            $file['fid'], $node->nid, $file['description'], $file['filepath'], $file['filemime'], $file['filesize']);
    return (array)$file;
  }
  else {
    // Include file name in upload error.
    form_set_error(NULL, t('file upload was unsuccessful.'));
    return FALSE;
  }
}

If you now upload the file and change the description of the file it lists it as the text specified

phl3tch’s picture

Cool. Wish I could remember why I was trying to do this though...

cmurockstar’s picture

I found the location for setting the link to the file in filefield_formatter.inc
return '

'. $icon . l($file['filename'], $url) .'

';

however, i am unclear how to get at the description of the file to replace 'filename'.... any help for a new Drupal 6 user?

cmurockstar’s picture

So you change $file['filename'] to $file['data']['description']

weedoo’s picture

There is the Token module that seems to be working when it comes to renaming files paths. I was looking at the filefield_file_insert function and found this piece of code in it ;)

  // allow tokenized paths.
  if (function_exists('token_replace')) {
    global $user;
    $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user);
  }
  else {
    $widget_file_path = $field['widget']['file_path'];
  }

So I guess in the field settings you can just use the accepted tokens and they will be changed at the saving step.