php 中上传文件的散列内容

hashing content of uploaded file in php

我有一个上传表单,旁边还有一个 Dropzone 文件上传器,用户可以在其中上传他们的 .csv 文件。

我想检查文件是否是新的。 检查应该包含文件内容是否是新的

我将文件名存储在数据库中以检查新文件,但问题是当用户重命名同一文件并再次上传时。

我的解决方案是散列每个文件的内容并将其存储在数据库中的文件名旁边。

但我不知道如何对 php、Laravel 中的 .csv 文件的内容进行哈希处理。 我试过使用

hash_file('md5' , Request::file('myFileName')->getClientOriginalName());

但它导致找不到文件或流。

无论名称如何,检查新文件的正确方法是什么?

提前致谢。

计算文件的哈希值是正确的方法。

做这样的事情:

public function uploadFile() {
    // check if upload is valid file (example: request()->file('myFileName')->isValid() )

    // calculate hash of a file
    $fileHash = sha1_file( request()->file('myFileName')->path() );

    //search for file in database and block same file upload (Files is Elequent model)
    if( Files::where('hash', $fileHash)->firstOrNull() != null) {
        abort(400); // abort upload
    }

    // do something with file and save hash to DB
}