首页 > 解决方案 > 如何在 laravel 中使用播种机保存特定文件 .docx

问题描述

我想用播种机将文件.docx保存在laravel的mysql数据库中,,

这是我在 DocumentTableSeeder 中的代码

 public function run()
{
    DB::table('document')->insert([
        'filename' => file_get_contents(database_path() . '/seeds/SOW.docx'),

    ]);

    DB::table('document')->insert([
        'filename' => file_get_contents(database_path() . '/seeds/RFQ.docx'),

    ]);

    DB::table('document')->insert([
        'filename' => file_get_contents(database_path() . '/seeds/TKDN.docx'),

    ]);

}

文档表中的列文件名是字符串,但我收到这样的错误 在此处输入图像描述

如何使用播种机 laravel 将 .docx 文件保存在 mysql 数据库中?请帮助谢谢

标签: laravel

解决方案


您可以将列类型更改为longText,可能文件的内容被截断。

并使用htmlenties函数将文件内容转换为安全内容。

public function run()
{
    DB::table('document')->insert([
        'filename' => htmlentities(file_get_contents(database_path() . '/seeds/SOW.docx')),
    ]);

    DB::table('document')->insert([
        'filename' => htmlentities(file_get_contents(database_path() . '/seeds/RFQ.docx')),
    ]);

    DB::table('document')->insert([
        'filename' => htmlentities(file_get_contents(database_path() . '/seeds/TKDN.docx')),
    ]);
}

我不知道你的逻辑,但你不应该将文件内容保存到数据库中,你应该保存文件路径而不是文件内容。


推荐阅读