Page 1 of 1
How do I stop duplicate graphics being uploaded
Posted: Mon Jun 02, 2008 10:42 am
by martin7jd
Hi all,
I can upload multipul graphics to my website, that works fine.
What I would like to stop is duplicates as shown in the array below. Any suggestions as to how I can do this
Array ( [userfile7] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [userfile6] => Array ( [name] => 12345678.cdr [type] => image/pjpeg [tmp_name] => /tmp/phph1v9DW [error] => 0 [size] => 626568 ) [userfile5] => Array ( [name] => 12345678.cdr [type] => image/pjpeg [tmp_name] => /tmp/phpzI5BBJ [error] => 0 [size] => 626568 ) )
Thanks
Martin
Re: How do I stop duplicate graphics being uploaded
Posted: Mon Jun 02, 2008 9:59 pm
by hansford
you mean you dont want someone uploading a file with the same name as one thats already in the directory?
Or you dont want someone uploading files which have the same name as one of the other files they are uploading?'
Or both? hehe
let the user go ahead and hit the submit button-just check the filenames before you allow it to be submitted-if they have duplicates - give them an error. I used an alert in this example, but you could write it to a DIV or anything you want. The script is just an example - to put into production you might want to add some more validation to see if what they are loading is really a valid file name type etc..
----------------------------------------
<form name='form1' enctype='multipart/form-data' method='post' action='process.php'>
Choose a file to upload:<input type="file" name="file01"/><br>
<input type="file" name="file01"/><br>
<input type="file" name="file02"/><br>
<input type="file" name="file03"/><br>
<input type='button' value='submit' onclick='validate(this.form)'>
</form>
<script language='javascript'>
function validate(frm){
var imagearray = new Array();
for(var i = 0; i < (frm.length - 1); i++){
if(frm.value == "") { continue; }
var file = frm.value;
var patharray = file.split("\\")
var len = patharray.length;
len += - 1;
imagearray = patharray[len];
//alert(imagearray);
}
var img = "";
var img2 = "";
var duplicate = 0;
for(var i = 0; i < imagearray.length; i++){
if(duplicate) { break;}
img = imagearray;
for(var j = 0; j < imagearray.length; j++){
img2 = imagearray[j];
if(j == i){
continue;
}
if(img == img2){
alert('ERROR: Duplicate fileanames');
duplicate = 1;
break;
}
}
}
if(!duplicate){
frm.submit();
}
</script>
Re: How do I stop duplicate graphics being uploaded
Posted: Mon Jun 02, 2008 10:06 pm
by hansford
sorry - forgot to add a closing } to the function.
also - you might want to check that they dont hit the submit button when they havent chosen anything to upload
Re: How do I stop duplicate graphics being uploaded
Posted: Mon Jun 02, 2008 10:21 pm
by hansford
~pickle: wrap your code in appropriate code tags please
heres the fixed script with the added validation to make sure they dont try to submit the form whn they havent chosen a file to upload
---------------------------
Code: Select all
<script language='javascript'>
function validate(frm){
var imagearray = new Array();
for(var i = 0; i < (frm.length - 1); i++){
if(frm[i].value == "") { continue; }
var file = frm[i].value;
var patharray = file.split("\\")
var len = patharray.length;
len += - 1;
imagearray[i] = patharray[len];
//alert(imagearray[i]);
}
var img = "";
var img2 = "";
var duplicate = 0;
var nofile = 1;
for(var i = 0; i < imagearray.length; i++){
if(imagearray[i] != ""){
nofile = 0;
}
}
if(nofile){
alert('ERROR: You must choose a file to upload');
return;
}
for(var i = 0; i < imagearray.length; i++){
if(duplicate) { break;}
img = imagearray[i];
for(var j = 0; j < imagearray.length; j++){
img2 = imagearray[j];
if(j == i){
continue;
}
if(img == img2){
alert('ERROR: Duplicate fileanames');
duplicate = 1;
break;
}
}
}
if(!duplicate){
frm.submit();
}
}
</script>
~pickle: wrap your code in appropriate code tags please