Laravel 5 load multiple images

I am trying to upload multiple images at once. The files are uploaded, but they pass the check even if they are not valid iamge. I want to download all valid files and return a message with the number of files that were not downloaded successfully.

My function:

public function addImages($id, Request$request)
{
    $files = Input::file('images');
    $uploaded = 0;
    $failed = 0;
    foreach ($files as $file) {

        // Validate each file
        $rules = array('file' => 'required|image');
        $validator = Validator::make(array('file'=> $file), $rules);

        if($validator->passes()) {
            $destinationPath = 'uploads';
            $filename = $file->getClientOriginalName();
            $upload_success = $file->move($destinationPath, $filename);
            if($upload_success){
                $uploaded ++;
            }
            else {
               $failed ++;
            }
        } else {
            $failed ++;
        }
    }
    \Session::flash('success_message',$uploaded.' files uploaded.');
    if($failed > 0){
        \Session::flash('errors','Some files werent valid.');
    }
    return redirect('admin/myroute/'.$id);
}

      

For some reason, if I download files without any extension, they download anyway. What am I doing wrong?

+3


source to share


2 answers


You don't have to check for it in the controller, you have to use every method for it, for example in your request file.

protected function getValidatorInstance()
{
    $validator = parent::getValidatorInstance();


    $validator->each('files', ['image']);

    return $validator;
}

      

It just checks all files.



Additional Information,

Laravel API about validation

+2


source


if you take over the validation, you have to specify as file types, you can specify the name and do the validation like this 'image' => 'mimes: jpeg, jpg, bmp, png'. Hope this helps



-1


source







All Articles