added a comment - - edited
Hi
I am a member of Moodle UFBA (Universidade Federal da Bahia - Brazil) team.
Here we had this problem.
I solved this for version 1.8 but I think this can fix the problem for other versions too.
The solution is simple... when trying to upload a file, an AJAX script makes a request to the server to check if the file exists or not. If true, a javascript confirm box asks the user if he wants to overwrite the file or not.
I don't know what exactly I must do to show this fix... by the time, the fix is shown below:
This javascript is added at files/index.php, just above the form printing:
<script type="text/javascript">
function file_exists(name) {
try {
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { /* do nothing */}
var long = name;
var broken = name.split("/");
var filename = broken.pop();
filename = filename.split(".");
var ext = filename.pop();
var just_name = filename.pop();
var id = '.$course->id.';
dest = "file_exists.php?id="id"&name="just_name"&ext="+ext;
xmlhttp.open("GET", dest, false);
xmlhttp.send(null);
if (xmlhttp.responseText==1) {
var overwrite = confirm(\'FIle \'just_name\'.\'ext\' already exists. Overwrite?\');
if (overwrite) {
return true;
} else {
return false;
}
} else {
return true;
}
}
</script>
At the form tag, we add: onsubmit=\"return file_exists(document.forms[0].userfile.value)\">
This is the PHP script called by the AJAX request to check if the file exists. Meaninfully, it is called file_exists.php and must be located at the folder files/ :
<?php
require_once("../config.php");
require_once("../lib/moodlelib.php");
$dir = $CFG->dataroot;
$name = $_GET['name'];
$name = clean_filename($name);
$id = $_GET['id'];
$ext = $_GET['ext'];
$file = $dir."/".$id."/".$name.".".$ext;
if (file_exists($file)) {
echo 1;
} else {
echo 0;
}
?>
I will check the way I must pass through to make this available (of course, if approved)
From Gustav Delius (gwd2 at york.ac.uk) Saturday, 8 May 2004, 09:21 PM:
I think this is quite an important annoyance but I have no good idea of what to do about it. Of course if we already had the new Moodle DMS with is version control ...