1

I beginner in php and developing APIs for my android project. I am trying to upload file in php,In Postman the API is working fine but when i try to upload file from android it isn't being uploaded. here its my php code.it shows undefined index: image. From android i'm sending MultipartEntity

$app->post('/addBanner', 'authenticate', function () use ($app) {
global $user_id;
$response = array();
//verifyRequiredParams(array('title', 'address', 'description'));
$title = $app->request->post('title');
$address = $app->request->post('address');
$description = $app->request->post('description');
$admin = $app->request->post('admin');
$type = $app->request->post('type');

$response = uploadFile("/uploads/banners/", $_FILES['image']);
//pr($file_name);die;
$res = 5;
if (!empty($response['imageName'])) {
    $image = $response['imageName'];
    unset($response['imageName']);
    $db = new DbHandler();
    $res = $db->insertBanner($title, $address, $description, $admin, $image);
}

if ($res == 0) {
    $response["error"] = false;
    $response["message"] = "Complaint successfully added";
} else if ($res == 1) {
    $response["error"] = true;
    $response["message"] = "Oops! An error complaint not add";
}
echoRespnse(200, $response);
});

Here it is uploadFile() function

function uploadFile($dir,$file)
{
$headers = apache_request_headers();

$target_dir = __DIR__ .$dir;


$uploadOk =1;
$file = $file;
$fileName=basename(time().md5($file['image']).$file['name']);
$target_file = $target_dir.$fileName;
$imageFileType =strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(file_exists($target_file)){
    echo "sorry, file already exists.";
    $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) {
    $response["error"] = true;
    $response["message"] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
if($uploadOk==0){
    $response["error"] = true;
    $response["message"] = "sorry, your file was not uploaded";
}else{
    if(move_uploaded_file($file["tmp_name"],$target_file)){
        $image=$fileName;
        $response["imageName"] = $fileName;

    }else{

        $response["error"] = true;
        $response["message"] = "Sorry, there was an error uploading your file.";
    }
}
return $response;
}

and here i'm inserting in database

public function insertBanner($title, $address, $description, $admin, $image)
{
    $response = array();
    $stmt = $this->conn->prepare("INSERT INTO banners(title,address,description,admin,image) values(?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $title, $address, $description, $admin, $image);
    $result = $stmt->execute();
    $stmt->close();
    // Check for successful insertion
    if ($result) {
        return COMPLAINT_CREATED_SUCCESSFULLY;
    } else {
        return COMPLAINT_CREATE_FAILED;
    }

}

This is what i am getting in response.

$_FILES DATA
Array
(
)
 $_REQUEST DATA
Array
(

)

And the following error,

 Slim Application Error

 The application could not run because of the following error:DetailsType: ErrorExceptionCode: <br >
 8Message: Undefined index: imageFile: /Applications/XAMPP/xamppfiles/htdocs/ech/v1/index.phpLine: 295<br >
 Trace<pre>#0 /Applications/XAMPP/xamppfiles/htdocs/ech/v1/index.php(295): Slim\Slim::handleErrors(8, 'Undefined index...', '/Applications/X...', 295, Array)
DarkBee
  • 16,592
  • 6
  • 46
  • 58
DMK
  • 13
  • 5
  • It seems that problem is in your Android code. – Niklesh Raut Jan 05 '18 at 06:53
  • but it is throwing `undefined index: image` – DMK Jan 05 '18 at 06:57
  • Seems that notice is raised not in code you provided. Check which file corresponds for this part and put it here. – Yupik Jan 05 '18 at 07:00
  • can you please print this before the function cal of upload and send me the result here print_r($_FILES); print_r($_REQUEST); – Umar Majeed Jan 05 '18 at 07:03
  • I have two files one is `index.php` and second is `dbHelper.php` `addBanner()` is in `index.php` and `insertBanner()` is in `dbHelper.php` – DMK Jan 05 '18 at 07:04
  • @OmerMuhammad I updated the code with response. – DMK Jan 05 '18 at 07:20
  • the error is in your android application beacuse your are not getting anything from android to php code print_r($_FILES)print_r($_REQUEST) will print all the data you will receive from the app. – Umar Majeed Jan 05 '18 at 07:26
  • I am posting `multipartEntity` so is this code supposed to handle the `MultipartEntity`? – DMK Jan 05 '18 at 07:29
  • 1
    https://stackoverflow.com/questions/14380827/php-for-receiving-image-and-text-from-multipartentity here is your answer – Umar Majeed Jan 05 '18 at 07:37

0 Answers0