0

I'm storing images in a folder, and saving their path encoded to base64 in database. The problem is, my images don't display, only their 'broken icons'. I don't know what I'm doing wrong there. Here's a stored image:

data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAQCAwMDAgQDAwMEBAQEBQkGBQUFBQsICAYJDQsNDQ0LDAwOEBQRDg8TDwwMEhgSExUWFxcXDhEZGxkWGhQWFxb/2wBDAQQEBAUFBQoGBgoWDwwPFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhb/wAARCAQ4B4ADASIAAhEBA

and my code:

<?php
require_once "db_connect.php";

if( isset( $_GET['id'])) {
    $id = $_GET['id']; 
} 

if ($stmt = $conn->prepare("SELECT image FROM cardimages WHERE id=?")) {

    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();


    while ($row = $result->fetch_assoc()) {
        $image = $row['image'];
        $image_src = "uploads/" . $image;
        echo $image_src;
    }


    $stmt->close();
}
$conn->close();
?>

and in the html:

<img class="card-img-top" src="imageView.php?id=<?php echo $row["id"]; ?>" /
  • It looks like you're confusing displaying a base64 encoded image directly as opposed to displaying an image via a file path. It should be either one or the other. – Jamie_D Mar 26 '20 at 14:28
  • I want to display it from the folder. What should I change? – kristof12301 Mar 26 '20 at 14:46
  • Without knowing how you store the image in your file path (folder), there's really know way to answer your question. Image files are stored in directories as a binary files with a file name (e.g. `yourfile.jpg`) . To display the image directly, see the answers from [this post](https://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php/16262187) – Jamie_D Mar 26 '20 at 14:51
  • I just wanted to store the path of each images, i thought I have to do this with base64, but looks like I'm storing the full image now. Am I right? – kristof12301 Mar 26 '20 at 14:57
  • 1
    That's correct. What you are storing in the database is a base64 string representation of the image and not the path with a file name. – Jamie_D Mar 26 '20 at 15:00
  • got it now, thank you. – kristof12301 Mar 26 '20 at 15:02

0 Answers0