Welcome Guest, Not a member yet? Register   Sign In
Outputting images stored in directories outside public root
#1

[eluser]MishaBear[/eluser]
Hello people -

I am rather new to CI, so I apologize if I missed this in the forums. I searched, but could not find it.

In my application, I am uploading images to a directory not available to the public. I need to write a script that reads the image content and outputs it (or, if the image was saved into the DB - an option available in the application - output it from the DB) without revealing the original image's path.

Is there some function in codeigniter to read an image and output it with the correct headers or I just create another controller for this?

Thanks!
#2

[eluser]umefarooq[/eluser]
if you are saving image in database are you saving is content type or header also or not. what is your table structure its better to save images in directory not in database .
#3

[eluser]TheFuzzy0ne[/eluser]
I'd create a controller. However, be advised that this means that the framework is instantiated every time you want to load an image, and could potentially put a lot of strain on your server.

I'm not entirely sure why you'd want to save the image in the database, though. Would it not make more sense to only index the images in the database instead? That way, the user won't ever see a path, just a hash.
#4

[eluser]Thorpe Obazee[/eluser]
I'd prefer to just index them in the database. I believe it be a problem to maintain something when images are stored in the database.

To answer your question, I believe you will need to out headers for the type of doc. (not really sure of it)
#5

[eluser]Myles Wakeham[/eluser]
Here's what I do (take from it what you will)...

Images are uploaded by user to a path accessible to PHP. On receipt, I create a record of the image in a database and store its original name in the db row. This is just an index. I take the sequence number generated by the DB and replace the filename (leaving the original extension) with it, and come up with a new name (ie. 123.jpg).

I then move the original uploaded image, to a place that I want it. Keep in mind, I'm a big believer in basedir security, so I keep the basedir range limited to protect my site from hack attempts.

After moving the file, I remove the original from its upload path. I then replace all references to the image to its new file name. This renaming guarantees unique file names on the server, since I can't control if 2 users upload the same 'car.jpg' file, or whatever.

It works fine. The real benefit is basedir security. If you saw the number of hack attempts on my code before this was introduced, it would blow your mind....

Myles
#6

[eluser]Gordaen[/eluser]
Are only images being stored in the directory (in other words, no security issues to worry about)? If so, I'd just create a symlink. Loading the framework for every image you serve is a lot of overhead.
#7

[eluser]MishaBear[/eluser]
Guys, sorry for the late reply. Yes, just images stored in a directory. The database storage is just an option for those willing to use it. But I think Myles approach will do fine in my case Wink




Theme © iAndrew 2016 - Forum software by © MyBB