1

how can i prefix a name to the uploaded file name in drupal?

Eg: If my uploaded file name is 'example.pdf' after click upload, i want to rename it as 'myprefix_example.pdf'

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
TeamG
  • 1
  • 2

2 Answers2

1

Code example:

function [MODULE]_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == "MY_FORM_ID")
    {
        $form['#submit'] = 'my_new_submit_callback';
    }
}

function my_new_submit_callback($form, &$form_state)
{
    $uploaded_file = file_load($form_state['values']['my_file_field']);
    $uploaded_file->name = "suffix_" . $file->name;
    file_save($uploaded_file);
}

The code isn't tested, let me know if it didn't work.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
0

Are you storing the file using the file field? If so, check out the "File (Field) Paths" module. http://drupal.org/project/filefield_paths

Ivan Zugec
  • 81
  • 8