4

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

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.

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.

Thanks, K

1 Answer 1

5

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.


The hashing answer

Laravel has a method on the file uploader called hashName() that according to the API docs generates "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:

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 does this to me too sometimes. This question might 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Look up the source for the hashName() method, if I am not mistaken, it does no such md5 of the contents. It simply returns Str::random(40) as the hash part.
@Jonathan you are right, it looks like this was changed in Laravel 5.4 after this answer was posted. I will update it accordingly.
Thanks, I think you clearly answered my original question, I will be able to use your method as i needed it in one of my Lravel5.1 project, but excel is still an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.