如何在 CodeIgniter 中显示数据库中的图像?
我正在使用 CodeIgniter 2.1.0 和 MySQL 数据库.我已经通过表单上传了一张图片并成功地将其存储在上传目录中,并且我还成功地将图片的完整路径存储在我的数据库中.但我无法通过从我的数据库中调用完整路径来显示图像.
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
它不能作为您机器上的本地路径使用.我的文件系统上不会有那个图像.该图像需要可在网络服务器上访问.(就像你的 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:
上传/_1_.jpg
然后执行 <img src="<?php echo $data['screenshot'];?>"/>
或将图像存储为:
_1_.jpg
和然后做
<img src="<?php echo sprintf("uploads/%s", $data['screenshot']);?>"/>
要清楚:您存储它的位置是正确的.但是,您不需要数据库中的完整路径,您只需要 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.
相关文章