1
votes

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 ?

1
What MIME you are getting in var_dump ?Rayon
an array of get name, size etc, i can share the exact values if you like tosaimcan
Have you tried this pathinfo($target_file,PATHINFO_EXTENSION) ???Rayon
Not yet, i'll feedback immediatelysaimcan
@Rayon Dabre, what should $target_file include ? because it needs a string value. By the way, i uploaded my post with var_dump($_FILES); return value.saimcan

1 Answers

0
votes

It was a easy question and found the easy solution for it.

$file = (Input::file('fieldName-0'));

Got the input, validated.

Thanks.