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

Php 如何将图像文件上载到特定人员的数据库?,php,database,laravel,migration,Php,Database,Laravel,Migration,首先,你好,我的朋友 我想根据用户显示具有特定图像的用户配置文件 我试过这个定义,但它不起作用。你能告诉我怎么做吗 public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('surname');

首先,你好,我的朋友

我想根据用户显示具有特定图像的用户配置文件

我试过这个定义,但它不起作用。你能告诉我怎么做吗

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));
除非您拒绝其他图像类型,否则我也会保存它的扩展名

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

然后将其显示在img标签中

'data:image/' . $user->image_type . ';base64,' . $user->image
备注:image/format是图像的mime格式

另一种方法是,您仍然可以在laravel中以本机方式保存图像,而无需使用此软件包,如果您在其他模型中使用图像,文档非常清晰明了,因此我将把这一步骤留给您


问题是什么?不要以明文形式存储密码。当我注册时,我想在我的个人资料中上载个人资料图片。@cugurel您有两个选择。使用base64编码并存储为字符串或存储为文件。Praveen Kumar,你能告诉我怎么做吗?感谢介绍Spatiale/laravel medialibrary软件包,我以前使用过,它非常棒。