I can't use Input::file('elementName') because i need to post file through jQuery for dynamic purposes.
I post selected file with FormData()
var data = new FormData();
jQuery.each(jQuery('#fieldName')[0].files, function(i, file) {
data.append('fieldName-'+i, file);
});
jQuery.ajax({
url: 'postFile',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
console.log(data);
}
});
Then i get the data with $_FILES function.
But when i try to use a laravel validator, it doesn't work for me:
//$file is not null and can get the values with var_dump
$file = $_FILES['fieldName-0'];
$fileValidator = Validator::make(
array('fileValidation' => $file),
array('fileValidation' => 'required|mimes:png,jpg,bmp,pdf|max:5000')
);
var_dump($_FILES) returns when uploaded a random file:
array(1) {
["fieldName-0"]=>
array(5) {
["name"]=>
string(7) "aaa.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(14) "/tmp/php4jQw3L"
["error"]=>
int(0)
["size"]=>
int(35438)
}
}
Result : validation always fails for mime types. i also tried image/png image/jpg image/bmg and application/pdf too instead of png jpg bmp pdf.
What do you offer ? At least, is there a way to post a file with jQuery in a formatted way to get with laravel Input::file format ?