使用form class
将图片上传到我的存储空间的最佳方式是什么?
刀片:
<form class="" action="{{route('newproduct')}}" method="post">
<div class="form-group">
<div class="form-group">
<label for="myimage"> <img src="/img/{{$user->myimage}}" height="80" width="80"></label>
<input type="file" id="myimage" name="myimage"></font>
</div>
</div>
控制器:
public function NewProduct(Request $request){
$user = Auth::user();
$user->myimage= date('mdYHis') . uniqid() . $request->myimage;
$user->save();
return redirect()->back();
}
在我的控制器中,它只保存数据库中随机生成的名称,但不要将文件上传到我的服务器。我想使用此路径来保存路径:
/public/img/
更新了没有图像路径的代码:
public function NewProduct (Request $request)
if($request->hasFile('myimage')){
if ($request->file('myimage')->isValid()) {
$user = Auth::user();
$image_name = date('mdYHis') . uniqid() . $request->file('myimage')->getClientOriginalName();
$user->save();
return redirect()->back();
}
}
由于
答案 0 :(得分:0)
首先,将enctype="multipart/form-data"
添加到<form>
然后,在你的控制器中添加:
public function NewProduct(Request $request){
if($request->hasFile('myimage')){
if ($request->file('myimage')->isValid()) {
$user = Auth::user();
$image_name = date('mdYHis') . uniqid() . $request->file('myimage')->getClientOriginalName();
$path = base_path() . '/public/img';
$request->file('myimage')->move($path,$image_name);
$user->myimage = $path.$image_name;
$user->save();
return redirect()->back();
}
return redirect()->back()->with('error', 'image not valid');
}
return redirect()->back()->with('error', 'no image');
}
对于多个图像,只需迭代myimage数组:
public function NewProduct(Request $request){
if($request->hasFile('myimage')){
// Do a loop over all images
foreach ($request->file('myimage') as $image) {
if ($image->isValid()) {
$user = Auth::user();
$image_name = date('mdYHis') . uniqid() . $image->getClientOriginalName();
$path = base_path() . '/public/img';
$image->move($path,$image_name);
$user->myimage = $path.$image_name;
$user->save();
return redirect()->back();
}
}
// end loop
}
return redirect()->back()->with('error', 'no image');
}
这种方法的一个问题是用户将拍摄最后一张图片(所有图片都会上传,但用户图片路径将是最后一张)。
可以解决创建一个表来存储用户图像路径并将图像保存在那里(一对多关系)
答案 1 :(得分:0)
刀片:
<form action="{{route('newproduct')}}" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="form-group">
<label for="myimage">
<input type="file" id="myimage" name="myimage"></font>
</div>
</div>
</form>
控制器:
use Illuminate\Support\Facades\Storage;
public function NewProduct(Request $request){
// if request has file
if($request->hasFile('myimage')){
$filenameWithExt=$request->file('myimage')->getClientOriginalName();
$filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
$extension=$request->file('myimage')->getClientOriginalExtension();
$fileNameToStore= date('mdYHis') . uniqid() .$filename.'.'.$extension;
request()->myimage->move(public_path('img'), $fileNameToStore);
}else{
// if no file from request , then put one no-image.jpeg in public/img folder
$fileNameToStore='no-image.jpeg'
}
$user = Auth::user();
$user->myimage=$fileNameToStore;
$user->save();
return redirect()->back();
}