There is a method on the request which will tell you if a file has been attached:
$request->hasFile('th_attach');
Then get the file using
$request->file('th_attach');
I am using the below code to upload a file.. If I select the file to upload, it is working fine but If i did not select a file then it is throwing error message
Call to a member function getClientOriginalName() on null
My Code
if($request->th_attach->getClientOriginalName())
{
$ext = $request->th_attach->getClientOriginalExtension();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->th_attach->storeAs('public/categories',$file);
}
else
{
$file='';
}
In some case I can not upload files.
@bhhussain this should do it:
$filename='';
if( $request->hasFile('th_attach'))
{
$file = $request->file('th_attach');
$ext = $file>getClientOriginalExtension();
$filename = date('YmdHis').rand(1,99999).'.'.$ext;
$file->storeAs('public/categories', $filename);
}
// file here should be set to the full path ie: "public/categories/$filename"
Let me know :)
Please or to participate in this conversation.