Currently the IMCE editor has the option to replace an existing file with the new one in the settings located at:
admin/settings/imce

The feature works correctly but while working on a project it was requested of me to have the editor prompt the user to confirm that they would like to overwrite the existing file before actually overwriting the file. I was able to do this by modifying the javascript and using drupal_add_js and drupal_to_js to make the "imce_settings_replace" variable available to the javascript layer. I have created a patch that modifies inc/imce.pages.inc and js/imce.js to acheive this result.

After thinking about this request it seemed like something that might be useful to the community and might be good as default functionality of the IMCE module so I am attaching the patch to this issue for review.

Files: 
CommentFileSizeAuthor
#1 imce_prompt.patch126.2 KBblischalk
imce_prompt.patch1.58 KBblischalk

Comments

StatusFileSize
new126.2 KB

Upon further testing it was discovered that the prompt was not functioning in IE properly. I have updated the patch so that it functions properly in both IE and Firefox. I am attaching the updated patch with this post.

Status:Active» Needs review

Setting thread to "Needs review" status.

Subscribe

You don't have to hack IMCE for this. You just need to define hook_form_alter for imce upload form and put a checkbox(Overwrite existing?) and add a validator to the form. The validator should check the value of checkbox and change the value of $GLOBALS['conf']['imce_settings_replace'] accordingly.

Status:Needs review» Closed (won't fix)

Version:6.x-2.0-rc1» 7.x-1.5

Any chance we could get a version 7.x version of this patch? Or even more details about about how to make ufku's changes?

I agree a 7.x version would be nice.

This pretty well does it for me in D7

<?php
function imcevalid_form_imce_upload_form_alter(&$form, &$form_state) {
 
drupal_add_js('jQuery(document).ready(function() {
      jQuery("#edit-upload").click(function(e) {
        fname = jQuery("#edit-imce").val();
        var newfile  = fname.substring(12).toLowerCase();
        jQuery("#file-list td.name").each(function() {
          tableobj = jQuery(this)
          var value = tableobj.find("span").html();
          if (value == newfile) {
             if (!confirm("You are about to replace an older file!  Continue?")){
               e.preventDefault();
               return;
             }
          }
        });
      });
    });'
,
    array(
'type' => 'inline', 'scope' => 'footer', 'weight' => 5)
  );
}
?>

Where did you add that code?

I created a new module named "imcevalid"

so the folder imcevalid contains:
imcevalid.info
imcevalid.module

imcevalid.info:

name = IMCE File Upload Validator
description = prompts user before replacing files
package = imce
core = 7.x
files[] = imcevalid.module

imcevalid.modul contains just the function i posted above (without

<?php

?>
tags):

<?php
function imcevalid_form_imce_upload_form_alter(&$form, &$form_state) {
 
drupal_add_js('jQuery(document).ready(function() {
      jQuery("#edit-upload").click(function(e) {
        fname = jQuery("#edit-imce").val();
        var newfile  = fname.substring(12).toLowerCase();
        jQuery("#file-list td.name").each(function() {
          tableobj = jQuery(this)
          var value = tableobj.find("span").html();
          if (value == newfile) {
             if (!confirm("You are about to replace an older file!  Continue?")){
               e.preventDefault();
               return;
             }
          }
        });
      });
    });'
,
    array(
'type' => 'inline', 'scope' => 'footer', 'weight' => 5)
  );
}
?>

Upload this module and enable it...should be good to go....

Let me know if you need more info.

Also...if your file names aren't already lowercase you may need to change the javascript to do that before comparing the new file with the existing ones.

For some reason I can't get this new module to work. I've created the files as you described, enabled the module and changed my IMCE common settings to "Replace the existing file with the new one" but I don't get a prompt or anything else when trying to upload a file already on the server.

I use CamelCase for my file names, so because of that I tried changing

var newfile  = fname.substring(12).toLowerCase();

to

var newfile  = fname.substring(12);

but this still didn't fire the prompt. Am I missing something?

One final note, I had to keep in the opening php tag in the module. Without it, it just dropped the function at the head of my pages. (I kept the closing one off, however.)

After playing around with this script for a bit, this is what I got to finally work the way I was expecting it to.

<?php
function imcevalid_form_imce_upload_form_alter(&$form, &$form_state) {
  drupal_add_js('jQuery(document).ready(function() {
      jQuery("#edit-upload").click(function(e) {
        fname = jQuery("#edit-imce").val();
        var newfile  = fname.substring(0);
        jQuery("#file-list td.name").each(function() {
          tableobj = jQuery(this)
          var value = tableobj.find("span").html();
          if (value == newfile) {
             if (!confirm("You are about to replace an older file!  Continue?")){
               e.preventDefault();
               return;
             }
          }
        });
      });
    });',
    array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
  );
}

What was happening was that the .substring(12) was cutting off the first 12 characters of the imported file name so the comparison would never match. I changed that to .substring(0) and then pulled out the .toLowerCase() since my files are not all lower case. Now, things work as expected.

Thanks for your help with this. My users are going to love it.

I had trouble splitting the filename because I was getting something like "C:\fakepath\filename.ext" then when i did str,split("\\") it failed so I just did the substring to get rid of the fake path.

Also this script may need some tweaking for cross browser support because the value returned from the file input differs.

in chrome C:\fakepath\filename
in firefox its just filename so i did this:

if (jQuery.browser.mozilla){
var newfile = fname.toLowerCase();
}
else {
var newfile = fname.substring(12).toLowerCase();
}

Naturally, I didn't check other browsers to see what they were doing. After checking Chrome, I see the fakepath stuff you mentioned and your tweak worked. I'm a little afraid to see what IE 8 and 9 do to the path.

IE9 appears to be the same as Chrome..not sure about other versions. Hooray for jQuery hacks!

IE8 also works like Chrome/IE9. I'm calling that good and pushing this live. Thanks again my Tolkien inspired friend.