Arnold Arnold - 11 months ago 54
PHP Question

how to display an image from folder with php?

Can you guys help me? I am trying to retrieve an image from folder but the image won't showed up.

Here's my code :

I'm using table "article" :

id int(100) auto increment,
title varchar(150),
imageName varchar(250) -> to store the file name,
image varchar(250) -> to store the image location address,
content text

Here's my folder structure:

enter image description here

(artikel_created.php) -> proccess the title, image and content of the article

$tit = mysqli_real_escape_string($con, $_POST['title']);
$cont = mysqli_real_escape_string($con, $_POST['content']);
$fileName = $_FILES['image']['name']; //get the file name
$fileSize = $_FILES['image']['size']; //get the size
$fileError = $_FILES['image']['error']; //get the error when upload
$tmp = $_FILES['image']['tmp_name'];

<?php include 'admin_header.php'; ?>

<section class="body">
<div id="log"></div>
<div id="artikel_created_panel">

if ($fileSize > 0 || $fileError == 0) {
$move = move_uploaded_file($tmp, '../assets/news_image/' . $fileName);
if ($move) {
$result = mysqli_query($con, "INSERT into articles(title, imageName, image, content) VALUES('$tit','$fileName','assets/news_image/$fileName', '$cont')");
if (!$result) {
trigger_error("Query Failed! SQL: $result - Error: " . mysqli_error($con), E_USER_ERROR);
} else {
echo "<br/>";
echo "Artikel added succesfully";
echo "<br/>";
<a href=../admin/admin_panel.php>Back to admin panel</a>

and here's (artikel.php) to display the article

if (isset($_GET['id'])) {
$id = $_GET['id'];
$qry = mysqli_query($con, "SELECT * FROM articles WHERE id=$id");
if (!$qry) {
trigger_error("Query Failed! SQL: $qry - Error: " . mysqli_error($con), E_USER_ERROR);

/*Fetching data from the field "title"*/
while ($row = mysqli_fetch_array($qry)) {
echo "<h2>" . $row['title'] . "</h2>";
echo "<img src=" . $row['image'] . " />";
echo "<p>" . $row['content'] . "</p>";

Here's the image:

enter image description here

Can you guys tell me what's wrong with the code please?

Answer Source

Seems path issue.

I can see that you are inserting image path in table something like 'assets/news_image/$fileName' but you are uploading image '../assets/news_image/'.

Update file path to :-

For absolute path (recommended) :

echo "<img src="."/". $row['image'] . " />";


For relative path:

echo "<img src="."../". $row['image'] . " />";

Hope this will help you (y).