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

查看:53
本文介绍了考虑到我的图像链接存储在 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天全站免登陆