简体   繁体   中英

Display image from database Codeigniter

i store profile images in database by adding this code to upload process

($config['upload_path'] = './img/photos/';
$data['img'] = $config['upload_path'].$image_data['file_name'];).

In my database there is img column that has such paths

./img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg .

how can i display image in my view file? Thanks in advance.

echo $image_data['file_name']; 

看看您得到什么,然后添加缺少的路径。

Add this path with your base_url() . In your view file:

$url = base_url().$img;
<img src="<?=$url?>" />

You can also check your URL in browser before putting it in image tag.

For more dynamic, I would suggest to change

'./img/photos/';

to

'img/photos/';

It would put your file in the same directory then your file path would be nicer "img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg"

To display your image you could :

$url = base_url().$image_path; //http://domain.com/img/photos/8f8eeb37d5fd82b7ba4cd16a356064ce.jpg
<img src="<?php echo $url;?>"/>

Do not store image with path in your database table. Store only image name in your img column of the table. For path, you can make one constant. Form that constant, you can retrieve image from the path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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