0

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: enter image description here

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.

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

if i wanted to split up the PHP code and the HTML code do I just do a include phpcode.php at the top?
Yes, you could do that.
it works now except it echos out the path at the top of the page.
Make sure that you don't have any echo in your phpcode.php file.
1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.