Ad

How To Upload Photo From Flutter App To Server Via PHP?

- 1 answer

Hello I'm try to upload my photo from flutter app to server it's work when I'm using on Xampp but when I'm try to change to real server It didn't work I want to know why

This is my API for upload photo to Server

<?php
error_reporting(E_ERROR | E_PARSE);

// Response object structure
$response = new stdClass;
$response->status = null;
$response->message = null;

// Uploading file
$destination_dir = "Foodpic/";
$base_filename = basename($_FILES["file"]["name"]);
$target_file = $destination_dir . $base_filename;

if(!$_FILES["file"]["error"])
{
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {        
        $response->status = true;
        $response->message = "File uploaded successfully";

    } else {

        $response->status = false;
        $response->message = "File uploading failed";
    }    
} 
else
{
    $response->status = false;
    $response->message = "File uploading failed";
}

header('Content-Type: application/json');
echo json_encode($response);

And this is my code in app

  Future<Null> uploadFoodPhoto() async {
String url = '${Myconstant().domain}/API/foodImg.php';
Random x = Random();
int i = x.nextInt(999999999);
String lmgName = 'Food$i.jpg';
try {
  Map<String, dynamic> map = Map();
  map['file'] = await MultipartFile.fromFile(file.path, filename: lmgName);
  FormData formData = FormData.fromMap(map);
  await Dio().post('$url', data: formData).then((value) {
    print('Res = $value');
    urlImage = '/Buudeli/Foodpic/$lmgName';
    print('ImageUrl = ${Myconstant().domain}$urlImage');
    insertToDB();
  });
} catch (e) {}}

Xampp and real server use same code but idk why it didn't work. It's always said "Res = {"status":false,"message":"File uploading failed"" If I miss something please let me know
Ps. My english not good sorry about that if u don't understand something please ask me to explain

Ad

Answer

Ok Im already know what i miss .... I forgot to change public permission on server.... Everything working fine now xD

Ad
source: stackoverflow.com
Ad