简体   繁体   English

Codeigniter图像显示

[英]Codeigniter image display

I am using CodeIgniter to build a simple image gallery. 我正在使用CodeIgniter构建一个简单的图像库。 I want gallery to be secure so I don't want anybody without permission to check my images. 我希望画廊是安全的,所以我不希望有人未经允许检查我的图像。

For example, if image location is http://www.mydomain.com/images/my_pic.jpg I don't want the image to be accessible when typing this url in browser but I want it to be reached through the function like http://www.mydomain.com/controller/function/mypic.jpg , where permission can be checked. 例如,如果图像位置是http://www.mydomain.com/images/my_pic.jpg, 我不希望在浏览器中键入此URL时可以访问该图像,但是我希望通过诸如http之类的功能访问该图像 ://www.mydomain.com/controller/function/mypic.jpg ,可以在此处检查权限。

How can I achieve this ? 我该如何实现?

Save your images not in public directory. 将您的图像不保存在公共目录中。

You have public directory, files in which are public and accessible by typing url in browser, and non-public folders, which can not be accessed by url. 您有公共目录,在其中可以通过在浏览器中键入url访问的公共文件和非公共文件夹(不能通过url访问)。

Also, CI has routing system, I bet, so you can't just type any route in browser and get access. 另外,我敢打赌,CI具有路由系统,因此您不能只在浏览器中输入任何路由并获得访问权限。

Well main is store images in non-public directory and load them in controllers. 好吧,主要是将图像存储在非公共目录中,并将其加载到控制器中。 For example /gallery/image/12 : controller gallery , action image and id is 12. You will get file 12.jpg from non-public folder /files/images/12.jpg for example. 例如/gallery/image/12 :controller gallery ,动作image和id为12。例如,您将从非公共文件夹/files/images/12.jpg获取文件12.jpg。

If you have MORE specified questions create another post with it. 如果您有更多指定的问题,请另外创建一个帖子。

You could protect the image directory by using an .htaccess file with the content: 您可以通过使用带有以下内容的.htaccess文件来保护映像目录:

DENY FROM ALL

And then use a regular controller to access the images. 然后使用常规控制器访问图像。 You need to change the file header, so the PHP-output gets interpreted as an image. 您需要更改文件头,以便将PHP输出解释为图像。

//CODE-IGNITER SPECIFIC STUFF

//INSIDE THE CONTROLLER FUNCTION FIRST CHECK USER PERMISSIONS, THEN ADD SOMETHING LIKE THIS:

$file = 'example.jpg'; //will basically end up being the argument which was passed to the controller

//define the output for the client as a jpg image
header('Content-Type:image/jpeg');
header('Content-Length: ' . filesize($file));

readfile($file); //reads the image file and send output to the client

To make this more clear, you pass the filename of the image as an argument to the controller, check if the user has permissions to access this particular file, and if that is the case, you let your controller return the image to the client. 为了更清楚地说明这一点,您将图像的文件名作为参数传递给控制器​​,检查用户是否有权访问该特定文件,如果是这种情况,则让控制器将图像返回给客户端。 So what happens is that PHP actually returns an image. 因此,发生的事情是PHP实际上返回了一个图像。

You can restrict the "IMAGES" folder from directly being accessed from URL. 您可以限制直接从URL访问“ IMAGES”文件夹。 You can assign 644 permission to the that folders you want to restrict. 您可以为要限制的文件夹分配644权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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