views:

251

answers:

5

What I really want to do is check if the same file exists before or after uploading the file, is there a efficient way to do it?

Well, that's it!

Edit: Actually it doesn't matter if its after or before, as long as it can detect dups.

+1  A: 

EDIT: Sorry didn't realize you said BEFORE... What you could do is use AJAX to query the server before the upload button is enabled.

Script:

function CreateRequest()
{
var xmlhttp = false;

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false;
 }
}

if (!xmlhttp && window.createRequest) {
 try {
  xmlhttp = window.createRequest();
 } catch (e) {
  xmlhttp=false;
 }
}

if(!xmlhttp)
{
 alert("Could not create XmlHttpRequest. Browser does not support.");
}

return xmlhttp;
}


function validate()
{
    xmlhttp = CreateRequest();
    xmlhttp.open( "GET", "checkfile.php?file=" + document.getElementById('upload').value );
    xmlhttp.send( false );
    if( xmlhttp.responseText == "YES" )
        alert( "File already exists" );
    else
        document.getElementById('go').disabled = false;
}

HTML:

<input id="upload" type="file" name="upload" onchange="validate()"/>
<input name="Reset" type="submit" disabled=disabled id="go" value="Go"/>

PHP:

$file = basename( $_GET['file'] );
if( file_exists( "uploads/$file" ) )
    echo "YES";
else
    echo "NO";
Kane Wallmann
As pointed out in other answers you cannot compare the files but this method will check if a file with the same name exists.
Kane Wallmann
+1  A: 

You cannot do that without using AJAX or flash, because PHP is responsible for the upload itself and your script starts at the point where the file has already been uploaded. I'll add the appropriate tags to your answer so someone can help you

soulmerge
+2  A: 

You can't check before uploading the file.
Once it's uploaded you could compare the file sizes then the MD5s of the files to check if they are the same.

Greg
I don't know why I said 'before' as it didn't really matter, thanks md5 seems good enough, is there a reason to check for file sizes for this purpose?
saint
The file size check is much faster - if the sizes are different you can skip the md5 because you know the files are different
Greg
A: 

You only have few choice :

  • If you want to know before the user upload, you ask him the md5 sum of the file, then server-side you check if there already is a file (really user friendly).
  • If your file has a unique name, you ask the name of the file before uploading (user friendly too)
  • You can't before the user uploaded the file (not really what you want...)

Pick the one you prefer !

Clement Herreman
A: 

Checking before uploading is, as others have pointed out before, not possible. But once the file is uploaded, you should compare the MD5 hash of the files.

Instead of calculating the MD5 hash of a file over and over again, you could consider keeping a little database in which you cache the results. That should be more performant.

middus