Reputation: 143
I try to get the image that is stored in a folder of my localhost, I do not understand what is the problem, the attribute in JSON IMAGEN get empty, I need to convert it here..
<?php
/**
* Obtiene todas las metas de la base de datos
*/
const ESTADO = "estado";
const DATOS = "negocios";
const MENSAJE = "mensaje";
const CODIGO_EXITO = 1;
const CODIGO_FALLO = 2;
require '../data/Gastos.php';
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// Manejar petición GET
$negocios = Gastos::getAllNegocios();
//Definir el tipo de la respuesta
header('Content-Type: application/json');
$imagesPath = '/localhost:8888/htdocs/';
if ($negocios) {
$datos[ESTADO] = CODIGO_EXITO;
foreach($negocios as $meta) {
// Push an entry in the new array, replacing raw image with base64-encoded
$imgFileContents = file_get_contents($imagesPath.'/'.$meta['IMAGEN']);
$datos["negocios"][] = array(
'IDNEGOCIO' => $meta['IDNEGOCIO'],
'NOMBREIMAGEN' => $meta['NOMBREIMAGEN'],
'IMAGEN' => base64_encode($imgFileContents),
'NOMBRENEGOCIO' => $meta['NOMBRENEGOCIO'],
'DESCRIPCION' => $meta['DESCRIPCION'],
);
}
print json_encode($datos,JSON_UNESCAPED_UNICODE);
} else {
print json_encode(array(
ESTADO => CODIGO_FALLO,
MENSAJE => "Ha ocurrido un error"
));
}
}
?>
Get empty in JSON
{"estado":1,"negocios":[{"IDNEGOCIO":"1","NOMBREIMAGEN":"img_1","IMAGEN":"","NOMBRENEGOCIO":"YARYAS","DESCRIPCION":"Descripcion1"},{"IDNEGOCIO":"2","NOMBREIMAGEN":"img_2","IMAGEN":"","NOMBRENEGOCIO":"Skizza","DESCRIPCION":"Descripcion2"}]}
It is the directoy
Upvotes: 0
Views: 41
Reputation: 26
I think it might be because you're $imagesPath URL is pointing to a directory incorrectly. In most localhost setups "htdocs" is already the default directory when travelling to the localhost server, so assuming its a relative URL it should be something like "http://localhost:8888/fotos/" not "http://localhost:8888/htdocs/fotos/".
Try:
$imagesPath = 'http://localhost:8888/';
Upvotes: 1