blue-turtle's avatar

validating an array of files sent by ajax

hello, I've got this ajax request, I tried several ways to validate photo array, but every time I got an internal server error

let data = new FormData();
data.append('text', $('#text').val());
data.append('photos', uploaded_files);
// uploaded_files is an array of images
​
$.ajax({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    url: '/profile/newPost',
    method: 'POST',
    data: data,
    contentType: false,
    processData: false,
    success: function(response){
        console.log("response: ",response); 
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("error: " + textStatus + ' : ' + errorThrown);
}

this is my controller, but it doesn't work for me, don't know why

$data = $request->all();
​
$validator = Validator::make(
  $data, [
  'photos.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
  ],[
    'photos.*.required' => 'Please upload an image',
    'photos.*.mimes' => 'Only jpeg,png and bmp images are allowed',
    'photos.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
  ]
);
​
if ($validator->fails()) {
  // Validation error.. 
}

could anyone help me please

0 likes
5 replies
Talinon's avatar
Talinon
Best Answer
Level 51

If you have an internal server error, it's likely not a validation failure, but something else. Have you checked your server log to see what the problem is?

Talinon's avatar

Check the end of your storage/logs/laravel.log file for the last error thrown from the server

blue-turtle's avatar

@TALINON - [2018-12-03 14:01:35] local.ERROR: Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files. this is one the logs.

Talinon's avatar

A google search for that error shows a lot of times it is caused by needing to increase the maximum file size upload configuration on your web server. Are you trying to upload large images? Might be the case if the upload is getting denied or cut off. I don't know what web server you're running, but check the configuration for settings such as upload_max_filesize or post_max_size

https://github.com/the-control-group/voyager/issues/2887

https://stackoverflow.com/questions/51329442/intervention-with-laravel-unsupported-image-type-provided-with-jpg

1 like

Please or to participate in this conversation.