Application Error 500 on simple image upload PHP page


#1

Hello,
I’m learning to work with APIs in android applications and using a free 000webhost account.
I have place a simple PHP file which receives Base64 encoded string and a file name and converts it back to an image and stores it on server.

Yesterday the file was working file and I was able to upload images via it. But today I have started getting an Application Error 500 on the page. However, I’m still able to upload images via Postman but sometimes my android application becomes unresponsive.

Any help is appreciated. Thanks

Code for the file`<?php

header('Content-type : bitmap; charset=utf-8');
if(isset($_POST['encodedString']))
{
	$encoded_string = $_POST['encodedString'];
	$image_name = $_POST['imageName'];
	$propertyId = $_POST['propertyId'];
	
	$decoded_string = base64_decode($encoded_string);
	
	$path = 'images/'.$image_name;
	
	$file = fopen($path, 'wb');
	
	$is_written = fwrite($file, $decoded_string);
	fclose($file);
	
	if($is_written > 0)
	{
		$connection = mysqli_connect('localhost', 'user', 'pass', 'db');
		$query = "INSERT INTO images(propertyId, imgUrl) values ('$propertyId', '$path')";
		
		$result = mysqli_query($connection, $query);
		if($result)
		{
			echo "success";
		}
		else
		{
			echo "failed";
		}
		
		mysqli_close($connection);
	}
}

?>`


#2

Hi @aimi95!

What is the URL on which you are receiving the issue?


#3

http://aiman-awp.000webhostapp.com/dreamplace/API/connection.php

I have two separate API files. One is responsible for sending data and this one is responsible for uploading images.
The data API is working fine thou. Only getting this error on this connection.php page.


#4

Use header('Content-type : bitmap; charset=utf-8', true); and the error will vanish :wink:


Hit CTRL+F5 to purge browser cache and reload the page after correcting the bug.


#6

The problem is fixed.
It was partially due to the header and partially due to a typo in the Database password.

Thank you so much for taking your time to help me :slight_smile: . Cheers


#7