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.
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.