繁体   English   中英

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

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

首先,嗨,我的朋友。

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

我尝试了这个定义,但没有用。 你能告诉我任何方法吗?

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();
    });
}

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

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

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

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

然后将其显示在img标签中

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

PS:图像/格式是图像的哑剧


这样做的另一种方法(您仍然可以在不使用laravel的情况下本机保存图像)将使用此程序包,以防万一您在其他模型中使用图像,文档非常清楚和直接,因此我将这一步留给您

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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