Laravel 文件上传和 File_hash 作为名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33476525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:36:55  来源:igfitidea点击:

Laravel File Upload and File_hash as name

filelaravelhash

提问by karmendra

I am able to upload a file successfully using Laravel Storage and FileSystem classes.

我能够使用 Laravel Storage 和 FileSystem 类成功上传文件。

My problem is I want to identify the file uniquely via its content.

我的问题是我想通过其内容唯一地标识文件。

I am thinking when I save the file on server I rename the uploaded file with hash of the content.

我想当我将文件保存在服务器上时,我用内容的哈希重命名上传的文件。

Question is is there a way to get hash of the files content. Another complexity is it is a excel file.

问题是有没有办法获取文件内容的哈希值。另一个复杂性是它是一个 excel 文件。

Note: I tried md5_file to use the file hash but for a xlsx file even if I save the file without making a sigle change the md5_file is not same.

注意:我尝试使用 md5_file 来使用文件哈希,但是对于 xlsx 文件,即使我保存文件而不进行单一更改,md5_file 也不相同。

Thanks, K

谢谢,K

回答by Josh Mountain

Note: as of Laravel 5.4 the hashName()function no longer generates the file name based on the content hash of the file. To accomplish this you will need to use md5_file()manually.

注意:从 Laravel 5.4 开始,该hashName()函数不再根据文件的内容哈希生成文件名。为此,您需要md5_file()手动使用。



The hashing answer

哈希答案

Laravel has a method on the file uploader called hashName()that according to the API docsgenerates "a filename for the file that is the MD5 hash of the contents". I used this recently in a project to do exactly what you are trying to do using that and md5_file(). Here is an example of how I accomplished it:

Laravel 在文件上传器上有一个方法,称为hashName()根据API 文档生成“文件的文件名,即内容的 MD5 哈希值”。我最近在一个项目中使用它来完成您正在尝试使用它和md5_file(). 这是我如何完成它的示例:

View

看法

<form method="POST" action="/controller" files="true" enctype="multipart/form-data">
    {!! csrf_field() !!}
    <input type="file" id="file-list" name="file-list[]" multiple="true" />
    <button type="submit">Upload Files</button>
</form>

Controller

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Image;
use App\Filename;
use Storage;

class ImageController extends Controller
{
    /**
     * Store an uploaded file.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $disk = Storage::disk('images');

        foreach ($request->file('file-list') as $file) {

            $filename = Filename::first();

            $disk->putFile('', $file);

            Image::create([
                'filename' => $filename->name,
                'title' => $file->getClientOriginalName(),
                'extension' => $file->guessClientExtension(),
                'size' => $file->getClientSize(),
                'mime' => $file->getClientMimeType(),
                'hash' => md5_file($file->getRealPath()),
            ]);

            $filename->delete();
        }
    }
}


The Excel problem

Excel 问题

Excel does this to me too sometimes. This questionmight be related. I'm not sure there is much you can do here unless you have control over the uploads in which case you could just avoid opening them before you hash check.

Excel 有时也会这样对我。这个问题可能是相关的。我不确定您可以在这里做很多事情,除非您可以控制上传,在这种情况下,您可以避免在哈希检查之前打开它们。