我对如何在数据库表中保存图像内容感到困惑。
请看第四行代码。我确信这个restful方法(使用post)是有效的,因为getSize()返回了真值。
另外,如果我调试返回第5行的内容,我会得到类似于下一行的内容:
所以,我不确定我缺少什么来将这些数据保存到数据库中。

        $personId = Input::get('PersonId');
        $file = Input::file('media');
        $tmppath = $file->getRealPath();
        $content = file_get_contents($tmppath); //$file->getSize();

        // return $content;

        $model = Person::find($personId);
        $model->Photo = $content;
        $model->save();

        $result = array(
            "success" => true,
            "data" => $personId,
            "error" => ""
        );

最佳答案

您需要将文件保存到服务器,并且只将路径存储在数据库中。
编写适当的方法,理想情况下可以将其存储在trait中。

private function saveFileToDisk($file, $fileName)
{
    $path = public_path() . '/uploads/';

    return $file->move($path, $fileName . $file->getClientOriginalExtension());
}

然后,将文件输入传递给方法并提供文件名:
$model->Photo = $this->saveFileToDisk(Input::file('media'), $model->Name);

显然在这之前你需要验证你的输入。

关于php - 如何用 Eloquent 方法将图像文件内容保存到SQL数据库中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18623935/

10-12 16:26
查看更多