我的Laravel CRUD应用程序有问题。这是关于一个简单的示例联系人数据表。一切正常,直到我尝试上传一些图像。我的索引视图仅显示损坏的图像。我已经检查了路径,尽管我将路径设置为相同(当然,文件名除外),但有时会显示完整的随机路径。具有讽刺意味的是,表的最后一个图像并非如此。最后一行显示了预期的图像,我不知道为什么。
这是我用于文件上传的代码(嗯,关于此问题的整个存储功能):
public function store(Request $request)
{
$request->validate([
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required | email',
'picture'=>'max:2048'
]);
$contact = new Contact([
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'email' => $request->get('email'),
'job_title' => $request->get('job_title'),
'city' => $request->get('city'),
'country' => $request->get('country'),
'picture' => $request->file('picture')
]);
if ($files = $request->file('picture')) {
$destinationPath = 'public/image/'; // upload path
$profileImage = $files->getClientOriginalName();
$files->move($destinationPath, $profileImage);
$insert['picture'] = "$profileImage";
}
$contact->save();
return redirect('/contacts')->with('success', 'Contact saved!');
}
答案 0 :(得分:0)
public function store(Request $request)
{
$this->validate($request, [
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required | email',
'picture'=>'max:2048'
]);
$contact = new Contact;
$contact->first_name = $request->first_name;
$contact->last_name = $request->last_name;
$contact->job_title = $request->job_title;
$contact->country = $request->country;
if ($request->hasFile('picture')) {
$filename = $request->file('picture')->getClientOriginalName();//get image original name
$image_path = $request->file('picture')->move(public_path('image/'), $filename);//image save in public/image folder
$contact->picture = $filename;
}
$contact->save();
return redirect('/contacts')->with('success', 'Contact saved!');
}
}
在视图中调用图像路径src="{{asset('/image/'.$your_contact_image )}}"
<img src="{{asset('/image/.$your_contact_image')}}" height="50" width="50" style="float: left">