繁体   English   中英

考虑到我的图像链接存储在MySQL数据库中,如何通过php显示存储在文件夹中的图像

[英]How to display through php an image stored in a folder considering that my image's link is stored in MySQL database

作为一个好习惯,我只将图像的链接存储在数据库中,问题是:

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

c://image.jpg

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

我可以用这个吗:

$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'>";

谢谢大家

您只想将相对路径而不是绝对路径存储为链接到类似

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

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

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

  1. 我强烈建议您改用PDO。
  2. 如果需要将它们移动一天,请使用相对URL到图像文件夹。

这是一个例子。

// relative to your public webroot
$publicImageUrl = '/images/in/here';

// Pull up some record, maybe of a product 
$select = 'SELECT imageFilename FROM products WHERE id = 2332';
$results = mysql_query($select);
if(!$results) {
    // issue with query. deal with it here
} else {
    if( mysql_num_rows($result) ) {
        // record not found. deal with it here
    }

    $row = mysql_fetch_array($result);

    $imageSrc = $publicImageUrl  . '/' . $row['imageFilename'];
}

然后您的HTML将如下所示

<img src="<?php echo $imageSrc; ?>" />
  1. 使用PDO进行php <-> mysql连接

  2. 发布mysql查询输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM