I want to upload multiple file to sever using flutter and php, but only one file is being uploaded
Here is flutter code
var file = await http.MultipartFile.fromPath("file", _file[0].path);
var file2 = await http.MultipartFile.fromPath("file2", _file[1].path);
var file3 = await http.MultipartFile.fromPath("file3", _file[2].path);
request.files.add(file);
request.files.add(file2);
request.files.add(file3);
http.StreamedResponse response = await request.send();`
here is php code
<?php
$file = $_FILES['file']['name'];
$file2 = $_FILES['file2']['name'];
$file3 = $_FILES['file3']['name'];
$imagePath = 'upload/'.$file;
$imagePath2 = 'upload/'.$file2;
$imagePath3 = 'upload/'.$file3;
$tmp_name = $_FILES['file']['tmp_name'];
$tmp_name2 = $_FILES['file2']['tmp_name2'];
$tmp_name3 = $_FILES['file3']['tmp_name3'];
move_uploaded_file($tmp_name,$imagePath);
move_uploaded_file($tmp_name2,$imagePath2);
move_uploaded_file($tmp_name3,$imagePath3);
?>
I am trying to upload three files to server but only one file is uploaded
Answer
Solution:
I hope this will work
Map<String, String> headers = <String, String>{
"Content-Type": "multipart/form-data",
};
request.files.add(await http.MultipartFile.fromPath('file', file1));
request.files.add(await http.MultipartFile.fromPath('file2', file2));
request.headers.addAll(headers);
http.Response rootResponse = await http.Response.fromStream(
await request.send());
Source