Hi, I'm trying to display an image inside a php tag using a file path name I stored in mysql. So far not working.

<td><?php echo "<img src='images/$image'/>";?></td>

$image is the path to mysql. I can echo the path name so I know it works there but I can't display image.

Dani AI

Generated

Common causes when an image filename comes from a database are: the URL you build is not the same as the file location on disk (relative vs absolute path), the filename has stray whitespace or wrong case, the web server cannot read the file, or the filename contains characters that need encoding. A quick way to debug is to copy the final src value from the page source and open it directly in the browser; the network panel will show a 404/403 if the path or permissions are wrong.

A robust pattern is to validate the filesystem first, escape the final URL, and provide a fallback. For example:

<?php
$filename = trim($row['image_name']);         // value from DB
$webPath  = '/images/' . rawurlencode($filename);
$fsPath   = $_SERVER['DOCUMENT_ROOT'] . $webPath;

if ($filename !== '' && is_file($fsPath) && is_readable($fsPath)) {
    echo '<img src="' . htmlspecialchars($webPath, ENT_QUOTES, 'UTF-8') . '" alt="">';
} else {
    error_log('Missing image: ' . $fsPath);
    echo '<img src="/images/placeholder.png" alt="missing">';
}
?>

Troubleshooting checklist: confirm the exact filename on disk (watch case on Linux), remove leading/trailing spaces from DB values, url-encode names with spaces or special chars, verify file permissions (typical 0644), check for a stray <base> tag that would change relative paths, and inspect the browser network panel for HTTP status. If the image fails only in production, check .htaccess rules or hotlink protection.

Building on 's path hint and 's follow-up, validating the file system and escaping output prevents silent failures and XSS issues. For reference: PHP is_file and htmlspecialchars docs and the MDN <img> element page are useful starting points (is_file, htmlspecialchars, img element).

Recommended Answers

All 5 Replies

Try this:

<td><img src="<?php echo $image;?>"></td>

Thanks,
Tried it but no working. I do have the images stored in images folder and the name of the images themselves in the database

oopppssss. Sorry. I forgot to include the folder ^^

$images_folder = "images/";
<td><img src="<?php echo $images_folder.$image;?>"></td>
commented: correct answer +2

Thanks, still not working but I'm going to mess with it a bit. I like this approach better. Thanks again.

Thanks, I had one more problem with path name inside of database, works awesome now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.