简体   繁体   中英

How can i upload image file to database of specific person?

First of all hi my friend.

I want to display user profile with specific image according to user.

I tried this definition bu it did't work. Can you please show me any way to do this?

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('surname');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('title');
        $table->binary('image');
        $table->rememberToken();
        $table->timestamps();
    });
}

I would avoid storing user images in your database, so i'll propose two solutions, one is to save it inside the database, one is to actually store it on the filesystem and retrieve it (with a neat package)

$image_path = ''; // the path to the saved image
$image_type = pathinfo($image_path, PATHINFO_EXTENSION);
$image_binary = base64_encode(file_get_contents($image_path));

Unless you deny other image types i would also save it's extension

PS: Remember to add a field into the user table if you are going to save the image type

Then to display it in an img tag

'data:image/' . $user->image_type . ';base64,' . $user->image

PS: image/format is the mime of the image


The other way of doing it (you can still save images natively in laravel without using this) would be using this package, in case you would use images in other models, the documentation is pretty clear and straightforward so i'll leave that step for you

https://github.com/spatie/laravel-medialibrary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM