October 23, 2024
Chicago 12, Melborne City, USA
PHP

Can't upload multiple image using multipart request flutter


i’m currently trying to make a project that can upload multiple images in flutter, but when i use multipart request it’s only just can upload one image (last in list images),

i got this debug console when i try to print it:
[Instance of 'MultipartFile', Instance of 'MultipartFile', Instance of 'MultipartFile']

sorry for my bad english, can anyone help me?

my flutter project code:

Future<String> uploadPhotos(List<String> paths) async {
    Uri uri = Uri.parse('http://192.168.*.*/crud/upload_image.php');
    http.MultipartRequest request = http.MultipartRequest('POST', uri);

    for(String path in paths){
      request.files.add(await http.MultipartFile.fromPath('files', path));
    }
    http.StreamedResponse response = await request.send();
    var responseBytes = await response.stream.bytesToString();
    
    return responseBytes;
  }

upload_image.php

<?php
$uploads_dir = __DIR__ . '/uploads';
foreach ($_FILES as $field => $file) {
    if ($file['error'] === UPLOAD_ERR_OK) {
        $now = sha1(microtime(true));
        $tmp_name = $file["tmp_name"];
        $pathinfo = pathinfo($file['name']);
        while (is_file("{$uploads_dir}/{$field}_{$pathinfo['basename']}_{$now}.{$pathinfo['extension']}")) {
            $now .= '(1)';
        }
        move_uploaded_file($tmp_name, "{$uploads_dir}/{$field}_{$pathinfo['basename']}_{$now}.{$pathinfo['extension']}");
        echo "successfully uploaded {$field} ({$file['name']})" . PHP_EOL;
    } else {
        echo "failed to upload {$field} ({$file['name']})" . PHP_EOL;
    }
}
?>



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video