admin管理员组文章数量:1400117
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.
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.
Share Improve this question edited Jul 22, 2009 at 8:18 0xdeadbeef asked Jul 22, 2009 at 6:55 0xdeadbeef0xdeadbeef 4,1408 gold badges35 silver badges38 bronze badges6 Answers
Reset to default 4You can't check before uploading the file.
Once it's uploaded you could pare the file sizes then the MD5s of the files to check if they are the same.
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";
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
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 !
Checking before uploading is, as others have pointed out before, not possible. But once the file is uploaded, you should pare 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.
There is an even better solution for this.
If you just need to find out if two files are identical, paring file hashes can be inefficient, especially on large files. There's no reason to read two whole files and do all the math if the second byte of each file is different. If you don't need to store the hash value for later use, there may not be a need to calculate the hash value just to pare files.
A much faster solution can be found here: http://php/manual/en/function.md5-file.php#94494
To be 100% sure, you could still do the md5_file method afterwards.
本文标签: javascriptHow to check for duplicate files in phpStack Overflow
版权声明:本文标题:javascript - How to check for duplicate files in php? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744209074a2595332.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论