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

时间:2022-07-06 19:30:36

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数据库。我已经通过表单上传了一张图片并成功将其存储在uploads目录中,并且我还成功地将图像的完整路径存储在我的数据库中。但我通过从我的数据库调用完整路径显示图像有问题。

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?

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

3 个解决方案

#1


3  

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 src attribute, 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)

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

So you need the store the image as either this:

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

uploads/_1_.jpg

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

然后执行如何在CodeIgniter中显示数据库中的图像?”/>

Or store the image as:

或者将图像存储为:

_1_.jpg and and then do

_1_.jpg然后再做

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

如何在CodeIgniter中显示数据库中的图像?”/>

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.

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

#2


2  

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 : '';
 }

#3


1  

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"/> 

#1


3  

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 src attribute, 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)

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

So you need the store the image as either this:

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

uploads/_1_.jpg

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

然后执行如何在CodeIgniter中显示数据库中的图像?”/>

Or store the image as:

或者将图像存储为:

_1_.jpg and and then do

_1_.jpg然后再做

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

如何在CodeIgniter中显示数据库中的图像?”/>

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.

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

#2


2  

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 : '';
 }

#3


1  

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"/>