2
votes

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..

2
Perhaps $input['id'] ?Sulthan Allaudeen
found out that the problem is not with mp3 or mp4.. itz with the size i upload.. if it's too large then php has empty post variable it seems.. thats why i'm gettin Undefined index : ID..sathyadev
That's great. Then you shall post it as answer.Sulthan Allaudeen
still i don't know how to solve this.. do i need to change php upload settings.. can you please.. help me out by giving a fine answer..sathyadev
well i wanted to handle the error.. can u also say how to increase the size too.. thanks for the quick replies.. :)sathyadev

2 Answers

1
votes

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');;
}
0
votes

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