无法在 laravel 控制器的变量中获取文件名和扩展名?
Can't get file name and extension in variable in laravel controller?
无法在变量“$fileNameToStore”中获取文件名和扩展名,但可以在数据库中保存默认图像名称 {'noimage.jpg'}。
我想知道这是什么原因。
public function store(Request $request)
{
$this->validate($request, [
'img' => 'nullable|max:1999'
// 'phone' => 'required'
]);
//handle file
$fileNameToStore = 'noimage.jpg';
if($request->hasFile('img')){
//get file name and extension
$filenameWithExt = $request->file('img')->getClientOriginalName();
//get just file name
$filename = $request->file('img')->getClientOriginalName();
//get just extension
$extension = $request->file('img')->getClientOriginalExtension();
//file name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//upload the image
$path = $request->file('img')->storeAs('public/product_images', $fileNameToStore);
}
//create product
$product = new Product;
$product->type = $request->input('type');
$product->img = $fileNameToStore;
$product->details = $request->input('details');
$product->save();
$notification = array(
'message' => 'Ürün kaydedildi !',
'alert-type' => 'success'
);
return redirect('/urungir')->with($notification);
}
这个问题的原因是,在提交给控制器的表单中,有一个错误,如下所示, enctype="multipart/data"
是错误的,为了解决这个问题,应该改为: enctype="multipart/form-data"
// Wrong :
<form class="form-horizontal" action="/products" method="post" enctype="multipart/data" id="urungirform">
{{ csrf_field() }}
<input type="file" name="img">
</form>
// Correct :
<form class="form-horizontal" action="/products" method="post" enctype="multipart/form-data" id="urungirform">
{{ csrf_field() }}
<input type="file" name="img">
</form>
无法在变量“$fileNameToStore”中获取文件名和扩展名,但可以在数据库中保存默认图像名称 {'noimage.jpg'}。 我想知道这是什么原因。
public function store(Request $request)
{
$this->validate($request, [
'img' => 'nullable|max:1999'
// 'phone' => 'required'
]);
//handle file
$fileNameToStore = 'noimage.jpg';
if($request->hasFile('img')){
//get file name and extension
$filenameWithExt = $request->file('img')->getClientOriginalName();
//get just file name
$filename = $request->file('img')->getClientOriginalName();
//get just extension
$extension = $request->file('img')->getClientOriginalExtension();
//file name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//upload the image
$path = $request->file('img')->storeAs('public/product_images', $fileNameToStore);
}
//create product
$product = new Product;
$product->type = $request->input('type');
$product->img = $fileNameToStore;
$product->details = $request->input('details');
$product->save();
$notification = array(
'message' => 'Ürün kaydedildi !',
'alert-type' => 'success'
);
return redirect('/urungir')->with($notification);
}
这个问题的原因是,在提交给控制器的表单中,有一个错误,如下所示, enctype="multipart/data"
是错误的,为了解决这个问题,应该改为: enctype="multipart/form-data"
// Wrong :
<form class="form-horizontal" action="/products" method="post" enctype="multipart/data" id="urungirform">
{{ csrf_field() }}
<input type="file" name="img">
</form>
// Correct :
<form class="form-horizontal" action="/products" method="post" enctype="multipart/form-data" id="urungirform">
{{ csrf_field() }}
<input type="file" name="img">
</form>