I have images on my server that are uploaded by users within a group. I want to restrict the access to these images publicly by typing the absolute URL to the image. Access to these images should only be granted if the user is logged in to the system.
I am using CodeIgniter as a framework, and would like the restriction to be on the controller rather than the view. Right now I could not fetch these images on the view.
Here is my structure:
application
- <images_folder>
system
application
assets
assets > images_folder
system
Create a .htaccess file on this folder and type Deny from all
Create a controller to access these images
<?php
class Preview extends CI_Controller {
function show_image($image_filename) {
$img_path = <path_to_your_img_folder> . $image_filename;
$fp = fopen($img_path,'rb');
header('Content-Type: image/png');
header('Content-length: ' . filesize($img_path));
fpassthru($fp);
}
}
<img src="<?=base_url(preview/show_image/img.png)?>" />
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With