Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to force replacement of existing file with same name, if a new version is uploaded by user. I've seen a lot of posts about this topic but none that I could find addresses how to force the replacement.

I'm using a custom form, and here's the code I've been trying to make work during the form validation:

  $file = file_save_upload(
      'file', 
      array(
        'file_validate_is_document' => array(), // Validates file is really a document.
        'file_validate_extensions' => array('pdf doc ppt pps avi mpg mov wmv'), // Validate extensions.
      ),
      'public://',
      FILE_EXISTS_REPLACE
  );

The revised file (let's say "file.jpg") is uploaded, but inevitably Drupal creates a new copy and renames it file_0.jpg, file_1.jpg, etc for each new version.

I even tried to programmatically delete the original file using "file_delete" prior to committing the upload but I still get the copy!

Any help would be appreciated.

share|improve this question

1 Answer

up vote 2 down vote accepted

This is drupal's default behaviour and a known issue. It has been addressed a couple of ways.

The Upload File Replace module addresses this issue by swapping the filenames so the new file retains the original name.

The Media Update module allows in situ replacement of media files (part of the Media project).

You could either make use of one of these modules, or use them as a model for your own function.

share|improve this answer
Perfect, thanks @triskelion for all the options. I ended up using the Upload File Replace. – longboardnode Mar 30 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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