I make a Drupal Form using FAPI (Drupal 7) in this form I used managed_file field

$form['image_fid'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#upload_location' => 'public://',

I want to rename the uploaded file (make the filename = filename+date)

How can I make something like that

4 Answers 4


Assuming you know about file status, file_save & file_usage_add.

In your submit callback:

$file = file_load($form_state['values']['image_fid']);
$file->filename = 'somefilename';
$file->status = FILE_STATUS_PERMANENT;
file_usage_add($file, 'yourmodule', 'foo', 1);

More info at the Form API Reference.

  • 1
    It does not rename the file just change filename in database but I need to change it in hard disk and save the new uri in database
    – Ahmed
    Commented Sep 4, 2012 at 21:08
  • See $file->uri instead of filename. Haven't got a system to confirm atm.
    – Bart
    Commented Sep 4, 2012 at 21:15
  • thanks, but it does not rename the file
    – Ahmed
    Commented Sep 4, 2012 at 21:46
  • Did you figure this out in the end? Commented Sep 11, 2012 at 10:21

The best way to rename file is using https://www.drupal.org/project/filefield_paths module .

The File (Field) Paths module extends the default functionality of Drupal's core File module, Image module and many other File upload modules, by adding the ability to use entity based tokens in destination paths and file names.

In simple terms, File (Field) Paths allows you to automatically sort and rename your uploaded files using token based replacement patterns to maintain a nice clean filesystem.

also you can rename it programmatically with hook_file_insert

something like

function hook_file_insert($file)
    $parts = pathinfo($file->filename);
    if (in_array(mb_strtolower($parts['extension']), array('jpg', 'jpeg', 'gif', 'png', 'tif', 'flv', 'pdf'))) {
        $uri = 'public://' . 'dsfr_' . $file->uid . '_' . $file->timestamp . '.' . $parts['extension'];
        $file = file_move($file, $uri);

You can rename file on upload context through validator functions

here is the edited code

$form['image_fid'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#upload_location' => 'public://',
  '#upload_validators'=>  array('file_validate_name' => array()),

function file_validate_name(stdClass $file) {
  $errors = array();

  //generate file name 
  $new_filename = convert_filename_to_filename_date($file->filename);

  $file->filename = $new_filename;

  //changing file location 
  $destination_dir = "public://file_location/";

  //check if file exists. if exists rename it append incremental number  until the filename is unique

  $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME);

  // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and
  // there's an existing file so we need to bail.
  if ($file->destination === FALSE) {
    $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.',
    array('%source' => $file->source, '%directory' => $destination_dir));

  return $errors;

I have been struggling with this for a couple of days but this seems to be working for what I wanted to do:


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.