mhoreen's avatar

Laravel 8: File Upload with Original File Name and Extension

I currently have an API where it saves the uploaded image but it hashes it, turns it into strings but what I want to do now is to retain the original name of the image in the database, not the string-type.

Image Controller:

    $uploaded_files = $request->file->store('public/uploads/');
    
    $lesson = LessonIMG::find($id);
    $lesson->lesson_image = $request->file->hashName();

    $results = $lesson->save();
    if ($results) {
        return ["result" => "Image Added"];
    } else {
        return ["result" => "Image Not Added"];
    }

    return ["result" => $uploaded_files];
}

public function DeleteIMG($id)
{
    $lesson = LessonIMG::find($id);
    if (is_null($lesson)) {
        return response()->json('Record not found!', 401);
    }

    $lesson->update(['lesson_image' => null]);

    return response('Image Deleted', 200);
}

}

0 likes
1 reply
siangboon's avatar
Level 54
->getClientOriginalName()
2 likes

Please or to participate in this conversation.