feedback on code (upload library) |
[eluser]brian88[/eluser]
Here Im allowing a user to upload a max of 5 images and stores the image name in a database. Im most likely going to bump it up to 10 images. As you can see I have a lot of repeated code and its going to double when I go from uploading 5 images to 10 images. Everything works, but Im looking for feedback to make this code shorter and more simple for the controller and the view. heres what the database looks like...... if no picture is uploaded then the default is "nopic" id | name | image1 | image2 | image3 | image4 | image5 ---------------------------------------------------------------------------------------------------------- 1 | Dodge | dodge/pic1.png | dodge/pic2.png | dodge/pic3.png | dodge/pic4.png | nopic 2 | Ford | ford/pic1.png | ford/pic2.png | nopic | nopic | nopic */ controller Code: function index() { view Code: <div id='content'>
[eluser]craig.hoog[/eluser]
At the very least you could probably do a foreach loop and replace the number in each instance so that it loops through your files instead of doing this so many times.
[eluser]boltsabre[/eluser]
Set a variable at the start of your controller for the number of images (5 or 10 or whatever you want). Then where ever you find yourself repeating code for the images just use a loop using the afore mentioned variable as your total number of iterations Code: // controller Code: // your view You get the idea! This way you just have to change $num_of_images at the start of your controller and it changes everything else for you because you've got it all inside loops. (not tested, you may have to play around with the concatenation aspects of it, but the logic is sound). I haven't looked at the CI image uploader library, so I'm not sure how much of the security risks it covers, but you should research it. File uploads leave MASSIVE security holes. You should be doing such things as giving the images random image names, using PHP to "redraw" the image (it's possible to upload a file that an image extension but is actually a php or shell script, by redrawing the image, it checks against this. Then there is the double extension hack, users uploading .htaccess files to your image folders and much more - look into it otherwise you could find your site crashing - there are soooo many "out of the box" hacking programs that bored kiddies are using to crash sites just for the fun of it that you really cannot afford to not look into this. Just google "php file upload security" and you'll get some good hits! Good luck with it!
[eluser]PhilTem[/eluser]
You might also remove all fields named "image<int>" and make a new column called "images" where you store all images in a json_encode'd or serialized array. Then the looping is even easier than mentioned by @boltsabre. May a row be updated every once in a while or is it created once and never changed? With the latter case it's pretty easy to change your code, otherwise it's a bit more work, but not too much either. Scream if you need help
[eluser]brian88[/eluser]
Thanks. both are good answers. I did boltsabre's way. And now I wanna do PhilTem's way also. I made a new column "images" So now I can store all the trucks into this column with a serialized array? I showed all my code above but heres the model and updated controller code. Where can i serialize the data then unserialize for the view? model Code: function addTruck($data){ controller updated code Code: function index() { I tried Code: serialize( $db_data["images"] ) = $folder.'/'.$upload_data['file_name'];
[eluser]brian88[/eluser]
Also I added the loop in the view to make is more simple. It works for the form inputs at the bottom. But my for loop isnt working inside my foreach loops I get an error "Undefined property: stdClass::$image" Code: // this line isnt working view Code: <div id='content'>
[eluser]Sanjay Sarvaiya[/eluser]
[quote author="brian88" date="1337196196"]Thanks. both are good answers. I did boltsabre's way. And now I wanna do PhilTem's way also. I made a new column "images" So now I can store all the trucks into this column with a serialized array? I showed all my code above but heres the model and updated controller code. Where can i serialize the data then unserialize for the view? model Code: function addTruck($data){ controller updated code Code: function index() { I tried Code: serialize( $db_data["images"] ) = $folder.'/'.$upload_data['file_name']; Hey dude store file name in image array like this. Code: $db_data["images"][] = $folder.'/'.$upload_data['file_name'];
[eluser]brian88[/eluser]
Thanks. That helps. So now I get an error when inserting into the database. Code: Unknown column 'Array' in 'field list' So how or where do I serialize the array so the database accepts it? Code: // didnt work, error - Cannot use [] for reading
[eluser]Sanjay Sarvaiya[/eluser]
I get bug and fixed with this. Code: $data["images"] = serialize( $db_data["images"]); that may helps. |
Welcome Guest, Not a member yet? Register Sign In |