考虑到我的图像链接存储在MySQL数据库中,如何通过php显示存储在文件夹中的图像 [英] How to display through php an image stored in a folder considering that my image's link is stored in MySQL database

查看:105
本文介绍了考虑到我的图像链接存储在MySQL数据库中,如何通过php显示存储在文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为良好的做法,我只在我的数据库中存储我的图像链接,问题是:

As good practice, I'm only storing my image's link in my database, the questios are:

我应该如何存储图像的链接? (让我们说它在c上:)

How should I store my image's link? (let's say it's on c:)

c://image.jpg

我应该使用哪种PHP代码来显示该图像?
我只显示路径,我该怎么做才能显示图像?

Which peace of PHP code should I use to display that image? I'm only displaying the path, what should I do to display the image?

我可以使用这个:

$query = "SELECT ImageURL from WhateverTable";

$result = mysql_query($query) or die(mysql_error());

$Image = mysql_fetch_row($result);

echo "<img src='$Image[0]' alt='This is an image'>";

谢谢所有小伙伴

推荐答案

您只想存储相对路径,而不是绝对路径,如链接

You only want to store the relative path, not the absolute path, as linking to something like

 <img src="/var/www/vhosts/website.com/images/file.jpg"> 

将在任何真实网站上返回404。通过相对路径(/images/file.jpg)将文件存储在数据库中,如果它们都在同一目录中,则只存储文件名。

would return a 404 on any real website. store your files in the database via a relative path (/images/file.jpg) or by only the filename if they are all in the same directory.

或者,你可以学习MongoDB,它允许您实际将文件存储在数据库中。

alternatively, you can learn MongoDB and it allows you to actually store files in the database itself.

这篇关于考虑到我的图像链接存储在MySQL数据库中,如何通过php显示存储在文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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