【问题标题】:laravel application won't save images but stores its file name to data baselaravel 应用程序不会保存图像,而是将其文件名存储到数据库中
【发布时间】:2018-08-07 18:34:12
【问题描述】:

所以我使用的是 laravel 和 Intervention Image 包。 我的表单接受图片,将其名称保存到 DB 但永远不会保存图片。 为什么?

谢谢!

控制器:

public function store(Request $request)
{
    $img=Image::make($request->file('slika')); //slika-name of input field, and Slike my desired folder.
    $img->save('Slike');


    $this->validate($request,[
        'naziv'=>'required|string|max:30',
        'mesto'=>'required|integer|max:4',
    ]);

    $crs=new Crossroad();
    $crs->naziv=$request->naziv; //the name of the crossroad
    $crs->mesto=$request->mesto; //number of turns in it
    $crs->slika=$request->slika; //picture's name(string in DB)

    $crs->save();

    return redirect('/crossroads/index');
}

【问题讨论】:

  • 请不要发布代码图片。编辑您的问题并将代码发布到正文中。缩进4个空格,它会自动格式化。如果您能提供Minimal, Complete, and Verifiable example,那就太好了。
  • 请更改您的 Q 的标题。现在不提供任何信息。

标签: php html forms laravel webforms


【解决方案1】:

请试试这个:

if ($request->hasFile('slika')) {

$image = $request->file('slika');
$name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/path/to/');
$image->move($destinationPath, $name);

}

【讨论】:

  • 现在还有一个问题。在我的输入字段中,例如:“image1.jpg”,这是应该存储为文件名的字符串。相反,我得到这样的东西 D:\xampp\tmp\php23BD.tmp(这是文件的名称).. 那里可能有什么问题?
猜你喜欢
  • 1970-01-01
  • 2015-08-04
  • 2017-07-22
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多