如何在CodeIgniter中显示数据库中的图像? [英] How to display image from database in CodeIgniter?

查看:127
本文介绍了如何在CodeIgniter中显示数据库中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter 2.1.0和MySQL数据库。我已经通过表单上传了一张图片并成功将其存储在uploads目录中,并且我还成功地将图像的完整路径存储在我的数据库中。但是我通过调用我的数据库中的完整路径来显示图像时遇到问题。

I am using CodeIgniter 2.1.0 and MySQL database. I have uploaded an image through a form and successfully stored it in a uploads directory and I have also successfully stored the full path of the image in my database. but i am having problem with showing the image by calling the full path from my database.

这是我的上传代码:

$image_path = realpath(APPPATH . '../uploads');

$config = array(
    'allowed_types' => 'jpeg|png|gif|jpg', 
    'upload_path' => $image_path, 
    'max_size' => 2097152, 
    'overwrite' => TRUE, 
    'file_name' => '_' . $i . '_'
);

$this -> load -> library('upload', $config);

当我在我的数据库中存储图像的完整路径时,它看起来像这样

When I am storing the full path of the image in my database, it looks something like this

C:/wamp/www/my_project/uploads/_1_.jpg

如果我尝试

<img src="<?php echo $data['screenshot'];?>" />
//($data['screenshot'] refers to the image location retrieved from database)

这在我的视图文件中,没有显示图像。我究竟做错了什么?请有人告诉我。什么是标准程序?

this in my view file, no image is displayed. What am I doing wrong? Please someone tell me. What is the standard procedure?

推荐答案

在您的数据库中,如果我理解正确,您将图像存储为 C:/wamp/www/my_project/uploads/_1_.jpg

In your database, if i have understood correctly, you're storing the image as C:/wamp/www/my_project/uploads/_1_.jpg

所以当你回显图像路径时 img src 属性,你将

So when you're echoing out the image path the img src attribute, you will have

这不会像您的机器上的本地路径那样工作。我的文件系统上没有该图像。图像需要可以在Web服务器上访问。 (比如你的index.php文件)

which won't work as this as local path on your machine. I won't have that image on my file system. The image needs to be accessible on the webserver. (like your index.php file)

所以你需要将图像存储为:

So you need the store the image as either this:

uploads / _1_.jpg

然后执行< img src =<? php echo $ data ['screenshot'];?> />

或将图像存储为:

_1_.jpg
然后再做

_1_.jpg and and then do

< img src =<? php echo sprintf(uploads /%s,$ data ['screenshot']);?> />

编辑:要明确:您存储的位置是正确的。但是,您不需要DB中的完整路径,只需要Web服务器路径。

To be clear: Where you're storing it is correct. But, you don't need the full path in the DB, you just need the web server path.

这篇关于如何在CodeIgniter中显示数据库中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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