I am trying to upload multiple images at once. The files are being uploaded but they pass the validation even if they are not a valid iamge. I want to upload all valid files and return a message with the ammount of files that werent uploaded succesful.
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 upload files withhout any extension they are still uploaded. What am I doing wrong?