Show image stored on server in php

0

I'm trying to make a photo gallery to learn a bit about programming in php, but I've been stuck when it comes to wanting to show the images.

The images are stored on the server and the path in the database. The problem is that all the div that shows the image, with title and description, is in php and I can not see the image too.

Deputy:

<?php

$consulta = "SELECT nombre, archivo, descripcion FROM imagenes ORDER BY RAND()";
$resultado = mysqli_query($conexion, $consulta) or trigger_error("error de consulta");

if($resultado){

while ($col = mysqli_fetch_assoc($resultado)){

	echo '<div class="col-md-4">';
	echo 	'<img src="'.$col['archivo'].'" alt="" class="img-rounded">';
	echo	 '<div class="caption">';
	echo 		'<h4>';
	echo 			$col['nombre'];
	echo 		'</h4>';
	echo 		'<small>';
	echo 			$col['descripcion'];
	echo 		'</small>';
	echo	 '</div>';
	echo '</div>';

}
$resultado->close();
}
$conexion->close();

?>

The name and description if it is shown, but I have tried in a thousand ways to show the image and nothing.

Thank you!

    
asked by Britba 31.08.2016 в 22:30
source

1 answer

2

I dare to say that the problem has to do with the route of your image as El Assiduo has told you in your question.

Let's give an example. Suppose we have the following route stored in our database:

C:/AppServ/www/ibroms/admin/properties/uploads/imgprinciple/02-07-2016-1467494058-luxorhome.jpg

And we can view it correctly through our Windows file browser:

However, when we enter the path in our label <img> , the image does not appear as we want it:

And it shows us the following error in the console: Not allowed to load local resource

This error is due to the fact that one may never be able to show images directly in the browser giving the location of the folder where it is stored, for that it is convenient to use a server (since you are doing it from a local server) .

Suppose that my server is called as follows (what comes by default when installing AppServ -similar to Xammpp- http://localhost/

Then our option would be to rename our routes with the following code and using the substr function, which it returns a part of the chain.

 $cadena = substr($reg['rutaImagenPrincipal'],15);
 $localhost = 'http://localhost/'.$cadena;

What does this function do? Well, taking our path C:/AppServ/www/ibroms/admin/properties/uploads/imgprinciple/02-07-2016-1467494058-luxorhome.jpg , analyze the string and from the character 15 the 'part', returning the following string: ibroms/admin/properties/uploads/imgprinciple/02-07-2016-1467494058-luxorhome.jpg and then concatenate it with the path that will open, resulting in: http://localhost/ibroms/admin/properties/uploads/imgprinciple/02-07-2016-1467494058-luxorhome.jpg

And being able, now yes, to correctly visualize our images in our local server.

If you have any questions or do not understand, do not hesitate to tell me, I will gladly help you. Success and good vibes!

    
answered by 01.09.2016 в 02:22