使用php在html模板文件中显示带有存储在数据库中的路径的图像 [英] Displaying an image with path stored in Database in a html template file with php

查看:32
本文介绍了使用php在html模板文件中显示带有存储在数据库中的路径的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是我将图像存储到服务器上的某个位置(不是 Web 根目录,出于安全目的),并且我将这些图像的路径存储在数据库中.我使用 php 和 pear 的 html_template_it 来显示我的页面.显示图像的唯一成功方法是使用

so my problem is that i have images storesd to a location on my server (not the web root, for security purposes) and i have the path to these images stored in the database. i ues php along with pear's html_template_it to display my pages. The only successful method of displaying the images has ben to modify the headers using

header("Content-type:image/type")
header("Content-Disposition:inline")
echo "<img src=readfile($image) />";

我的问题是这会破坏模板,当我显示图像时,模板消失了.任何人都可以就绕过此问题的最佳方法提出建议吗?

my problem is that this disrupts the template and whilst i get the image display, the template is gone. Can anyone advise on the best way to bypass this?

推荐答案

正确的做法是:

<img src="image.php?imageID=XYZ" />

在您的模板中,其中 XYZ 是数据库中图像的主键.然后你就有了 image.php 脚本:

in your template, where XYZ is the image's primary key in the database. Then you'd have the image.php script:

<?php

$id = $_GET['imageID'];
$sql = "SELECT path FROM images WHERE id='" . mysql_real_escape_string($id) . "'";
... run query, fetch path, etc...

header('Content-type: image/whatever');
readfile($path);

如果您在代码片段中输出 image/whatever 内容类型,那么您不能将图像包装在某些 HTML 中,因为浏览器会将您的脚本发送的所有内容解释为一个图像,并看到一个损坏的图像 - 这个星球上没有任何图像格式将 <img .... 作为它的前几个字节.

If you output an image/whatever content-type as you are in your code snippet, then you can't wrap the image in some HTML, as the browser will interpret EVERYTHING your script sends as an image, and see a corrupted image - no image format on the planet has <img .... as its first few bytes.

这篇关于使用php在html模板文件中显示带有存储在数据库中的路径的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆