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

时间:2017-11-07 19:58:09

标签: php database laravel migration

首先,我的朋友。

我想根据用户显示具有特定图像的用户个人资料。

我试过这个定义,但它没有用。你能告诉我任何办法吗?

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 个答案:

答案 0 :(得分:1)

我会避免在您的数据库中存储用户图像,因此我将提出两种解决方案,一种是将其保存在数据库中,一种是将其实际存储在文件系统中并检索它(使用整齐的包)

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

除非你否认其他图像类型,否则我也会保存它的扩展名

P.S:如果要保存图像类型,请记住在用户表中添加一个字段

然后将其显示在img标签

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

P.S:图像/格式是图像的模仿

另一种方法(你仍然可以在laravel中保存图像而不使用它)将使用这个包,如果你在其他模型中使用图像,文档非常明确和直截了当所以我会离开那一步为你

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