简体   繁体   中英

Pass image path from database to HTML image source using PHP

I have a pics table that has the path of an uploaded photo. I want to display that image if a user uploaded a photo. I have a users table that has a pic_id that references the pic_id in a pics table.

Table schema: 在此处输入图片说明

After a user uploads a photo I reference the pic_id in the user's table with the pic_id in the pics table, which also has the pictures name and path.

I want to display that uploaded photo inside a div if the user has uploaded a photo. If not I attribute a default photo to the user.

HTML Sample code:

<div id = "inner_menu_img">
    <img id = "pics" src = "getimage.php">
</div>

Inside by getimage.php:

<?php
session_start();
include 'db_connect.php';
    if(isset($_SESSION['pic_id']))
    {
        $pic_id = $_SESSION['pic_id'];
          $sql_select = "SELECT filepath FROM pics WHERE pic_id = '$pic_id'";
          $result = $conn->query("$sql_select");
          $filepath = $result->fetch_assoc();
          $path = $filepath['filepath'];
            $finfo = finfo_open(FILEINFO_MIME_TYPE);  // return mime type ala mimetype extension
            $mime_type = finfo_file($finfo, $path);
            finfo_close($finfo);
            switch($mimetype)
            {
                case "image/jpeg":
                header ('content-type: image/jpeg'); 
                echo '<img src = ".$path.">';
                break;

                case "image/png":
                header ('content-type: image/png'); 
                echo '<img src = ".$path.">';
                break;
            }  
    }
    else
    {
        echo "uploaded_images/default_photo.jpg";
    }
?>

I'm not sure if this is the correct way to do this as I am trying to self teach myself PHP. If there is an easier way to do this with AJAX or any other way I would be happy to give it a shot.

I would recommend using a Framework if you are working on a project. If you are only testing stuff this is fine. For testing purpose I would put the code before the HTML and just assign the path of the image to a variable then echo this path in the src of the image.

<?php
...
if(isset($_SESSION['pic_id']))
    ...
    $imageSrc = '/path/to/image.jpg';
} else {
    $imageSrc = '/path/to/default.jpg';
}
?>
<!doctype html>
<html>
<body>
    <img src="<?php echo $imageSrc ?>">
</body>
</html>

if you only want to return a string with the new image url or html do not set the content-type to any image format.
otherwise if you really want to send the image back that is uploaded see this post

In your case, I see two solutions:

  • on logging, create a session var with pic path and echo it in your src attribute simply
    • if you want to preserve your getimage script, don't return an html tag but load image in var and echo this var and content type of image, nothing else (search for image proxy pattern if you want to study it)

Good luck

In a better way:

<?php
...
if(isset($_SESSION['pic_id']))
    ...
    $imageSrc = $_SESSION['pic_path'];
} else {
    $imageSrc = '/path/to/default.jpg';
}
?>
<!doctype html>
<html>
<body>
    <img src="<?php echo $imageSrc ?>">
</body>
</html>

But if you are learning PHP don't start with a framework now, you must know POO before and know basic procedural aspects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM