使用PHP将图像路径从数据库传递到HTML图像源

时间:2022-11-23 11:04:36

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.

我有一个图片表,其中包含上传照片的路径。我想在用户上传照片时显示该图像。我有一个用户表,其pic_id引用pics表中的pic_id。

Table schema: 使用PHP将图像路径从数据库传递到HTML图像源

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.

在用户上传照片后,我使用pics表中的pic_id引用用户表中的pic_id,其中还有图片名称和路径。

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.

如果用户上传了照片,我想在div中显示上传的照片。如果不是,我将默认照片归属于用户。

HTML Sample code:

HTML示例代码:

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

Inside by getimage.php:

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

我不确定这是否是正确的方法,因为我正在尝试自学PHP。如果有更简单的方法使用AJAX或任何其他方式来做到这一点,我很乐意尝试一下。

4 个解决方案

#1


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.

如果您正在处理项目,我建议使用Framework。如果你只测试东西,这很好。出于测试目的,我会将代码放在HTML之前,只是将图像的路径分配给变量,然后在图像的src中回显此路径。

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

#2


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

如果您只想返回带有新图像的字符串url或html,请不要将内容类型设置为任何图像格式。否则,如果你真的想要发回上传的图像,请看这篇文章

#3


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)
    • 如果你想保留你的getimage脚本,不要返回一个html标签但是在var中加载图像并回显这个var和内容类型的图像,没有别的(如果你想学习它,搜索图像代理模式)

  • 在记录时,使用pic路径创建会话var并在src属性中回显它,只需要保留getimage脚本,不要返回html标记但是在var中加载图像并回显此var和内容类型的图像,没有else(如果你想学习它,搜索图像代理模式)

Good luck

#4


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

但是如果你正在学习PHP,现在就不要从框架开始,你必须先了解POO并了解基本的程序方面

#1


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.

如果您正在处理项目,我建议使用Framework。如果你只测试东西,这很好。出于测试目的,我会将代码放在HTML之前,只是将图像的路径分配给变量,然后在图像的src中回显此路径。

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

#2


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

如果您只想返回带有新图像的字符串url或html,请不要将内容类型设置为任何图像格式。否则,如果你真的想要发回上传的图像,请看这篇文章

#3


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)
    • 如果你想保留你的getimage脚本,不要返回一个html标签但是在var中加载图像并回显这个var和内容类型的图像,没有别的(如果你想学习它,搜索图像代理模式)

  • 在记录时,使用pic路径创建会话var并在src属性中回显它,只需要保留getimage脚本,不要返回html标记但是在var中加载图像并回显此var和内容类型的图像,没有else(如果你想学习它,搜索图像代理模式)

Good luck

#4


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

但是如果你正在学习PHP,现在就不要从框架开始,你必须先了解POO并了解基本的程序方面