简体   繁体   中英

Codeigniter image display

I am using CodeIgniter to build a simple image gallery. 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.

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.

Also, CI has routing system, I bet, so you can't just type any route in browser and get access.

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.

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:

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.

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

You can restrict the "IMAGES" folder from directly being accessed from URL. You can assign 644 permission to the that folders you want to restrict.

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