So I am testing methods to upload an image to a php server. One of the methods was to convert the image to a Base64
String and upload it and then decode it when needed.
When I uploaded a picture I noticed that there was a difference in the Strings. Some spaces were missing and some next lines were misplaced.
Before I sent the string to the server the string is okay and then it goes through a _POST
command in the php file and then it is inserted into the database using INSERT INTO
.
I suspect that the header that I use changes the string when I try to upload it. the header is /x-www-form-urlencoded
. I tried using /json
but if the json object is converted to string it changes the original string. So what header do I need to use so the string won't change? Or is there another method to upload a picture to a MySQL
database?
This is the method I use to upload the string to the server:
ImageString = getStringImage(bitmap);
String ImageUploadString="image="+ImageString;
final OkHttpClient client = new OkHttpClient();
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
RequestBody body = RequestBody.create(contentType, imageUploadString));
Request request = new Request.Builder().url(uploadURL).post(body).build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
try {
String json = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Edit: this is the php file that I use to insert the picture:
<?php
$response = array();
if (isset($_POST['image']))
{
$image = $_POST['image'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = $db->query("INSERT INTO images(image) VALUES('$image')");
if ($result) {
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "missing fields";
echo json_encode($response);
}
?>
For image uploads you can always go for multipart part upload rather than converting it into base64. If you are using retrofit, the implementation will be easier.
I am adding the php and the android code to do it. I assume you are using httpurlconnection and not retrofit.
PHP
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Android
fileInputStream = new FileInputStream(new File(_filePath));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(
connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name=\"fileToUpload\";filename=\""
+ _filePath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
// int contentLength = bytesAvailable+
// header.getBytes().length+footer.getBytes().length;
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
I also suggest you to use retrofit as a lot of network handling are done by the library itself.
Problem is, the data you are sending to the server is being encoded somehow by your HttpClient
.
you just need to decode it in php
code -
For an example,
Here I was sending device id to the server, look how I did url decoding -
if(isset($_POST['device_id']) && !empty($_POST['device_id']))
$data->device_id = urldecode($_POST['device_id']);