DoubleUp's avatar

How to give uniqid/hash value to saved file

Hello guys... I have a function that let a user to upload and save the file to the system and show the file in the table. The problem is whenever there is a duplicate name it will not appear in the database and the file will not show in the table. As the title says, how to give uniqid/hash value to saved file?

Here is my code:

public function updatepekerjaan(Request $request, $id){
        $pekerjaan = Pekerjaan::find($id);
        $pekerjaan->update($request->all());
        if($request->hasFile('gambar')){
            $request->file('gambar')->move('gambarpekerjaan/', $request->file('gambar')->getClientOriginalName());
            $pekerjaan->gambar = $request->file('gambar')->getClientOriginalName();
            $pekerjaan->save();


            // $file = request()->file('gambar');
            // $extension = $file->getClientOriginalName();
            // $destination = 'gambarpekerjaan/';
            // $filename = uniqid() . '.' . $extension;
            // $file->move($destination, $filename);
            // $new_file = new Pekerjaan();
            // $new_file->gambar = $filename;
            // $new_file->save(); << doesn't work
        }
        
        return redirect()->route('datapekerjaan')->with('message','Pekerjaan Berhasil diupdate!');
    }
0 likes
3 replies
martinbean's avatar

@doubleup Well if you stored files as per the docs, then Laravel would do this for you already: https://laravel.com/docs/9.x/requests#storing-uploaded-files

$path = $request->file('gambar')->store('gambarpekerjaan');

// $path will be a file name with a random hash

Also, you really want to look into validation, because right now you’re not doing anything and blindly passing anything the user submits to your model for saving, which is a huge security risk if you model has many fillable or un-guarded attributes.

You also have a bug where you’re not checking if the model exists before calling methods on it. This line:

$pekerjaan = Pekerjaan::find($id);

Is going to cause a crash if a user specifies the ID of a model that doesn’t exist, because $pekerjaan is going to be null but then you try and call update on it, so you’re going to end up with a “Trying to call method on null” error.

DoubleUp's avatar

@martinbean Hello thanks for the reply...

Regarding the validation method, I already tried changing the code like below

$pekerjaan->update($request->all());
        if($request->hasFile('gambar')){
            $request->file('gambar')->move('gambarpekerjaan/', $request->file('gambar')->hashName());
            $pekerjaan->gambar = $request->file('gambar')->hashName();
            $pekerjaan->save();

But the image/file doesn't show up in the table nor the file is hashed in the db...

martinbean's avatar

@DoubleUp Read the docs I linked you to. You’re still trying to write your own code when you don’t need to.

One line. That’s all you need.

1 like

Please or to participate in this conversation.