如何检查PHP中的重复文件?

时间:2023-01-24 11:28:23

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.

编辑:实际上,无论是之前还是之后,只要它可以检测到重复。

6 个解决方案

#1


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.

在上传文件之前无法检查。上传后,您可以比较文件大小,然后比较文件的MD5,以检查它们是否相同。

#2


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.

编辑:抱歉没有意识到你说过之前...你可以做的是在启用上传按钮之前使用AJAX查询服务器。

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";

#3


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

如果不使用AJAX或flash,则无法做到这一点,因为PHP负责上传本身,并且脚本从文件上传的位置开始。我会在你的答案中添加适当的标签,以便有人可以帮助你

#4


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).
  • 如果你想在用户上传前知道,你问他md5文件的总和,然后服务器端你检查是否已经有文件(真的用户友好)。

  • 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 !

选择你喜欢的那个!

#5


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.

正如其他人之前指出的那样,在上传前检查是不可能的。但是一旦上传文件,您应该比较文件的MD5哈希值。

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.

您可以考虑保留一个缓存结果的小型数据库,而不是一遍又一遍地计算文件的MD5哈希值。这应该更高效。

#6


There is an even better solution for this.

有一个更好的解决方案。

If you just need to find out if two files are identical, comparing 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 compare files.

如果您只需要确定两个文件是否相同,那么比较文件哈希值可能效率低下,尤其是在大文件上。如果每个文件的第二个字节不同,则没有理由读取两个完整的文件并进行所有数学运算。如果您不需要存储哈希值供以后使用,则可能不需要仅仅为了比较文件来计算哈希值。

A much faster solution can be found here: http://php.net/manual/en/function.md5-file.php#94494

可以在这里找到更快的解决方案:http://php.net/manual/en/function.md5-file.php#94494

To be 100% sure, you could still do the md5_file method afterwards.

为了100%确定,之后你仍然可以使用md5_file方法。

#1


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.

在上传文件之前无法检查。上传后,您可以比较文件大小,然后比较文件的MD5,以检查它们是否相同。

#2


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.

编辑:抱歉没有意识到你说过之前...你可以做的是在启用上传按钮之前使用AJAX查询服务器。

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";

#3


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

如果不使用AJAX或flash,则无法做到这一点,因为PHP负责上传本身,并且脚本从文件上传的位置开始。我会在你的答案中添加适当的标签,以便有人可以帮助你

#4


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).
  • 如果你想在用户上传前知道,你问他md5文件的总和,然后服务器端你检查是否已经有文件(真的用户友好)。

  • 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 !

选择你喜欢的那个!

#5


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.

正如其他人之前指出的那样,在上传前检查是不可能的。但是一旦上传文件,您应该比较文件的MD5哈希值。

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.

您可以考虑保留一个缓存结果的小型数据库,而不是一遍又一遍地计算文件的MD5哈希值。这应该更高效。

#6


There is an even better solution for this.

有一个更好的解决方案。

If you just need to find out if two files are identical, comparing 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 compare files.

如果您只需要确定两个文件是否相同,那么比较文件哈希值可能效率低下,尤其是在大文件上。如果每个文件的第二个字节不同,则没有理由读取两个完整的文件并进行所有数学运算。如果您不需要存储哈希值供以后使用,则可能不需要仅仅为了比较文件来计算哈希值。

A much faster solution can be found here: http://php.net/manual/en/function.md5-file.php#94494

可以在这里找到更快的解决方案:http://php.net/manual/en/function.md5-file.php#94494

To be 100% sure, you could still do the md5_file method afterwards.

为了100%确定,之后你仍然可以使用md5_file方法。