dpjo15650 2017-11-07 19:58
浏览 25

如何将图像文件上传到特定人员的数据库?

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();
    });
}
  • 写回答

1条回答 默认 最新

  • dongou0524 2017-11-07 20:59
    关注

    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

    P.S: 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
    

    P.S: 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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据