我无法将图像上传到public / images文件夹,也无法将图像名称保存到我的数据库中。 未定义变量:imageName ,已生成此错误
public function store(Request $request)
{
$adm_companies=new adm_companies;
$adm_companies->com_name=$request->cname;
$adm_companies->com_company_address=$request->caddress;
$adm_companies->com_country=$request->country;
//$imageName="";
if($request->hasFile('filename'))
{
$imageName = time().'.'.request()->
filename->getClientOriginalExtension();
request()->filename->move(public_path('images'), $imageName);
}
$adm_companies->com_logo=$imageName;
$adm_companies->com_phone1=$request->cphone1;
$adm_companies->com_phone2=$request->cphone2;
$adm_companies->com_fax=$request->fax;
$adm_companies->com_email=$request->emailid;
if($adm_companies->save())
{
echo "<script type='text/javascript'>
alert('Inserted successfully!', window.history.back())
</script>";
}
else
{
echo "<script type='text/javascript'>
alert('Insertion failed!',window.history.back())
</script>";
}
}
答案 0 :(得分:0)
如果没有文件上传,则会出现此错误。
检查是否已将enctype="multipart/form-data"
添加到表单中。
也可以
$imageName="";
if($request->hasFile('filename'))
{
$imageName = time().'.'.request()->
filename->getClientOriginalExtension();
request()->filename->move(public_path('images'), $imageName);
}
因此,如果没有文件上传,它将在该变量中存储空白。
如果文件字段是必填字段,我建议添加适当的验证。