Reputation: 43
ok.. i don't know how many of you had this problem in Laravel.. i could not find any solution for this.
i'm validating the uploaded image using Validator by setting rules with mime type jpeg, bmp and png. I flash an error message if it's not of these types.
It works fine for all file types, but when i upload an mp3 or mp4 it shows an exception in my controller.
MyImageController.php Code :
public function addImageDetails()
{
$input = Request::all();
$ID = $input['ID'];
$name = $input['name'];
$file = array('image' => Input::file('image'));
$rules = array('image' => 'required|mimes:jpeg,jpg,bmp,png');
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
\Session::flash('flash_message_error','Should be an Image');
return view('addDetails');
}
else {
//If its image im saving the ID, name and the image in my DB
}
This is the error that i get when i upload an mp3 or mp4
ErrorException in MyImageController.php line 25:
Undefined index: ID
validating all other file types like [.txt, .doc, .ppt, .exe, .torrent] etc..
Upvotes: 2
Views: 3303
Reputation: 11310
Once you Allocate the Request::all();
to the variable $input
$input = Request::all();
Then you should check whether it has any value
if (isset($input['ID']))
If the condition satisfies the you shall allow it to proceed further steps else you should return the view with your error message like the code given below.
if (isset($input['ID']))
{
$ID = $input['ID'];
$name = $input['name'];
$file = array('image' => Input::file('image'));
}
else
{
return view('addDetails')->with('message', 'Image size too large');;
}
Update :
Here you check for image type
if (isset($input['ID']))
{
if (Input::file('image'))
{
$ID = $input['ID'];
$name = $input['name'];
$file = array('image' => Input::file('image'));
else
{
return view('addDetails')->with('message', 'Uploaded file is not an image');;
}
}
else
{
return view('addDetails')->with('message', 'Image size too large');;
}
Upvotes: 1
Reputation: 14747
Try with this clean code:
public function addImageDetails(Request request){
$this->validate($request, [
'image' => ['required', 'image']
]);
//If its image im saving the ID, name and the image in my DB
}
There is a rule called image
that could be helpful for you in this situation
Upvotes: 0