Htaccess to rewrite images folder |
[eluser]flyenig[/eluser]
Hello, i have a script that uploads images, creates a hash for it, creates 3 directories, and puts it inside the last directory like so... Code: images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg (the "108_" is the user id) my reason for the sub folders is because i am creating an application that is looking at millions of photos being uploaded so they need to be spread across evenly.. so im trying to turn Code: images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg Code: user_images/108/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg how can i do that? i want that if someone wants to view the photo, they see the new directory in the url, not the one with 3 sub directories.. Thank you!!
[eluser]CroNiX[/eluser]
Just create a user_images controller? Do you need the user_id as a separate dir if your image filename starts with {user_id}_{filename}? If none of your image filenames will have an underscore in them except where it separates the user id, you can explode on that to get the user id along with the filename and do away with the extra directory. Then you could do something like: /application/config/routes.php (could also do this using _remap()) Code: $route['user_images/(:any)'] = 'user_images/view/$1'; Controller Code: class User_images extends CI_Controller {
[eluser]flyenig[/eluser]
thanks for the reply, i forgot to add i have a helper function that will return the images..... Code: function get_image($image_id,$type = '') so i dont think this would work because i cant call controllers to the view unless i do a "get_instance" function..... because i plan on doing something like this. Code: // the helper function returns the images this is why i think it would be best to do this with htaccess. any suggestions?
[eluser]CroNiX[/eluser]
The view would have Code: <img src="/user_images/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg" alt="" />
[eluser]flyenig[/eluser]
oh yea... duh lol. didnt get any sleep last night. thank you!
[eluser]flyenig[/eluser]
wait i remember why i thought this would be better in htaccess.... This site will have 1 million plus images, so i was thinking, yeah i can do this thru php but how efficient would this method be? Speed wise? because that means if i have 50 images on one page then it has to go to the controller function 50 times? it already has to go to the helper function 50 times. which is more efficient/less resource hog? this php method or doing a rewrite with htaccess?
[eluser]CroNiX[/eluser]
What is the significance of these dirs and is there any logic to them? /a3c/65c/297/ I don't know how you would translate that unless that is a constant, which I doubt. I assume you have many seemingly random dirs. Are you going to write a rewrite rule for each one? I just don't know how htaccess would be able to translate since those values most likely are stored in a database somewhere or there is some formula for figuring them out. I'm kind of assuming that once you have a filename like you say you want like: 108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg That you can somehow look it up to get the actual dir structure that the image is in like: images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg
[eluser]XMadMax[/eluser]
Try this rewriterule: RewriteRule ^images/([a-fA-F0-9]+)/([a-fA-F0-9]+)/([a-fA-F0-9]+)/([a-fA-F0-9]+)_([a-fA-F0-9]+)\.(jpg|png|gif)$ user_images/$4/$4_$5.$6 [L] Allow (images) / (3 subdirs in hexa) / (hexa) + _ + (hexa) + . extension |
Welcome Guest, Not a member yet? Register Sign In |