MySQL 如何在 CodeIgniter 中显示数据库中的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9177399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:03:19  来源:igfitidea点击:

How to display image from database in CodeIgniter?

mysqlimage-processingimageviewcodeigniter-2image-uploading

提问by Shabib

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.

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

Here is my code for the upload:

这是我的上传代码:

$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

If i try

如果我尝试

<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?

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

采纳答案by Flukey

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

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

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

因此,当您回显图像路径img src属性时,您将拥有

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)

这不能作为您机器上的本地路径。我的文件系统上不会有那个图像。图像需要可在网络服务器上访问。(就像你的 index.php 文件)

So you need the store the image as either this:

因此,您需要将图像存储为:

uploads/_1_.jpg

uploads/_1_.jpg

and then do <img src="<?php echo $data['screenshot'];?>" />

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

Or store the image as:

或将图像存储为:

_1_.jpgand and then do

_1_.jpg然后做

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

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

EDIT: 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.

编辑:要清楚:您存储它的位置是正确的。但是,您不需要数据库中的完整路径,您只需要 Web 服务器路径。

回答by groovekiller

Controller:

控制器:

function displayimage($Id=FALSE){
if ($Id)) 
{
    $image = $this->MMarches->getImage($Id);
    header("Content-type: image/jpeg");
    print($image);
}        }

Model:

模型:

function getImage($Id){
$data = '';
$Q = $this->db->query("SELECT photo FROM tableWHERE phptoID=".$Id);
if ($Q->num_rows())
{
    $data = $Q->row_array();
    $data = $data['MA_PHOTO']
    $Q->free_result();  
}
return $data;} 

Your View:

您的观点:

src="<?php echo site_url("controller_name/display_image/$image_id"); ?>" 

ALTERNATIVE MODEL:

替代模型:

function getImage($Id){
$Q = $this->db->query("SELECT photo FROM tableWHERE phptoID=".$Id);
   if ($Q->num_rows())       {
           $data = $Q->row_array();
           $data = $data['MA_PHOTO'];
           $Q->free_result();  
   }    
   $size = $data->size();        
   $ret = $data->read($size);     
   return (isset($ret)) ? $ret : '';
 }

回答by shihabudheen

For displaying images in web browser you have to give URL of that image rather than PATH of the image.

要在 Web 浏览器中显示图像,您必须提供该图像的 URL 而不是图像的 PATH。

Following code does not display any image

以下代码不显示任何图像

<img src="C:/wamp/www/my_project/uploads/_1_.jpg"/> 

Following code used URL of the image,Here localhost server name is given, you have to replace your server address with localhost.

以下代码使用了图像的 URL,这里给出了 localhost 服务器名称,您必须将您的服务器地址替换为 localhost。

<img src="http://localhost/www/my_project/uploads/_1_.jpg"/>