Laravel 5.4: hasFile () no longer works
This worked fine under Laravel 5.3.
I just upgraded to 5.4 and can no longer upload files using the method hasFile()
. Check is ignored
public function addPhotos(Request $request)
{
// dd($request) here shows a file in the files:FileBag
if ($request->hasFile('photo'))
{
// dd($request) here never prints,
this block is passed over as if the file doesn't exist
}
}
Any ideas?
Update
When you dump the file, this is what I get so you can see that something is loading ...
dd($request->file('photo'));
returns ...
array:1 [▼
0 => UploadedFile {#504 ▼
-test: false
-originalName: "test.jpg"
-mimeType: "application/octet-stream"
-size: 0
-error: 1
#hashName: null
path: ""
filename: ""
basename: ""
pathname: ""
extension: ""
realPath: "/Users/tim/Sites/myapp/public"
aTime: 1969-12-31 19:00:00
mTime: 1969-12-31 19:00:00
cTime: 1969-12-31 19:00:00
inode: false
size: false
perms: 00
owner: false
group: false
type: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
]
source to share
It turns out I was testing an invalid jpeg (how is that anything at all?), So I had to create a method that would handle this scenario.
Also, my enter key is an array that will allow one or more files to be used, so I had to handle that too ...
public function uploadIsValid($request)
{
foreach ($request->file('photo') as $file) {
if (!$file->isValid()) {
return false;
}
}
return true;
}
Then in my method addPhotos()
if ($this->uploadIsValid($request)) {
// process uploaded files here...
}
source to share
try using the following method rather $ request-> hasFile
$image = $request->file('picture');
if ($image->isValid()) //return true if the file has been uploaded with HTTP and no error occurred
{
$imageName = time().'.'.$image->getClientOriginalExtension();//assign name of image with time prefix and image extension
$destinationPath = public_path('/images');//assign destination path in public folder
$image->move($destinationPath, $imageName);
$user->image=$imageName;
if($user->save())
{
return Response::json(['status' => 1, 'message' => 'Image updated','imageName'=>$imageName]);
}
else
{
return Response::json(['status' => 0, 'message' => 'Image not updated']);
}
}
source to share
as per the docs https://laravel.com/docs/5.4/requests its still there. Are you sure you are doing it right?
source to share