图像在数据库laravel中另存为.tmp文件

时间:2019-10-16 16:20:23

标签: laravel

我正在尝试使用Laravel保存图像

图像以tmp文件的形式保存在数据库中,为什么呢?

图像在数据库中另存为C:\ xampp \ tmp \ php50B5.tmp

我该如何解决?

控制器:

a

型号:

  public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'body'=>$request->input('body'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),
            'index_image' => $request->file('index_image'),
            'header_image' => $request->file('header_image'),
            'text_image' =>$request->file('text_image'),
        ];
        $request->file('index_image' )->store('Images');
        $request->file('header_image' )->store('Images');
        $request->file('text_image' )->store('Images');
        Article::created($new_file_data);
    }

index.blade:

class Article extends Model
{
    protected $primaryKey = 'id';
    protected $guarded = ['id'];
}

3 个答案:

答案 0 :(得分:0)

尝试更改此内容:

$request->file('index_image' )->store('Images');

对此:

$file = $request->file('index_image');
$file->storeAs('Images', "my-cool-name." . $file->getClientOriginalExtension());

答案 1 :(得分:0)

$request->file('index_image' )是上载图像存储的路径,它是一个临时目录。当您调用->store()时,该方法将返回存储文件的新路径,但您没有将该路径传递给create方法。

这段代码将满足您的需求,我要做的就是将存储方法移至new_file_data分配

public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'body'=>$request->input('body'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),
            'index_image' => $request->file('index_image')->store('Images');,
            'header_image' => $request->file('header_image')->store('Images');,
            'text_image' =>$request->file('text_image')->store('Images');,
        ];
        Article::created($new_file_data);
    }

答案 2 :(得分:0)

这是针对因任何原因可能遇到此问题的人的答案。 在 HTML 中:

<input type="file" name="photo"/>

在控制器中:

if ($request->hasFile('photo')) {
    $info->img = $this->upload_img($request->file('photo'));
}

首先,代码中图像的input名称与数据库中图像字段的名称相同。我更改了图像输入名称,问题解决了。这就是为什么“照片”是随请求传递的临时文件。 “img”有路径。见laracast