访问Codeigniter中的图像,但将URL限制为非用户


Accessing the images in Codeigniter but restricting URL to non users

我的服务器上有一组用户上传的图像。我想通过键入图像的绝对URL来限制对这些图像的公开访问。只有当用户登录到系统时,才应授予对这些图像的访问权限。

我使用CodeIgniter作为一个框架,希望限制在控制器上,而不是视图上。现在我无法在视图中获取这些图像。

这是我的结构:

      application
                - <images_folder>
      system
  1. 将所有照片放在应用程序文件夹外的文件夹中

application
assets
assets > images_folder 
system

  1. 在此文件夹上创建.htaccess文件并键入Deny from all

  2. 创建一个控制器来访问这些图像


<?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);
    }
}
  1. 对上述功能创建适当的限制,即:检查用户会话等
  2. 通过控制器访问图像。根据你的观点写:

<img src="<?=base_url(preview/show_image/img.png)?>" />

希望这能帮助