Description:
From my android mobile application I am taking a photo with the camera whose Bitmap I encode in Base64 to send it to my .php service which decodes it to save the image on the server.
How do I encode from android:
private fun encodeImage(bm: Bitmap): String? {
val baos = ByteArrayOutputStream()
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val b = baos.toByteArray()
return Base64.encodeToString(b, Base64.DEFAULT)
}
I send the base64 with the name of "productImage"
How to decode from PHP to send to the server
$businessId= $_POST["businessId"];
$productImage = $_POST["productImage"];
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $productImage));
/* Nombre del archivo en carpeta */
$path = "productsimgs/"."b$businessId.jpg";
/* Ruta de la imagen (bd) */
$url = "productsimgs/"."b".$businessId.".jpg";
echo json_encode(file_put_contents($path,$data));
Location of my productsimgs folder:
The problem:
When I run the php service, the file_put_contents echo returns "false". Checking my productsimgs folder doesn't save the image. That could be happening?