Jebil Jebil - 7 months ago 19
SQL Question

Display Image from the path stored in mysql

I have uploaded the image into the folder and stored the path into the MySQL Database. The path has been stored and the image has been inserted into the folder successfully. But my problem is when i display the image from the path which is stored in db. It is not displaying. When i echo the image path, it displays the image path. I checked my browser settings, it's all ok. Here is my code. Please anybody help.

$up=move_uploaded_file($_FILES['profile']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/'.$name);
$path = dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/';
$location = $path . $_FILES['profile']['name'];
$ins=mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo "$location"; //It displays D:/xampp/htdocs/upload/image/Chrysanthemum.jpg
echo '<img width="250" height="250" src= "'.$location.'"/>';//It doesn't display anything.

Answer

Try this:

Try to use relative path not absolute path. The relative path helps you to run the script from any server, any directory.

$path = 'upload/image/';
$location = $path . $_FILES['profile']['name'];

move_uploaded_file($_FILES['profile']['tmp_name'], $location);

$ins = mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo '<img width="250" height="250" src= "'.$location.'"/>';

Note: you need to have a folder name upload/image in the same directory from where the script run. As your requirement, you want to show image from mysql, but did't do it, for that you need to query the table again.