Uploading an image with form_validation |
[eluser]Xeoncross[/eluser]
This is an updated version of the awesome topic by Jatinder Thind. I made some changes to support the new form_validation class and I registered "upload_data" about the uploaded file for use later (like you plan to resize the image or something). Because of the way it is setup, as soon as the form validation is run - it will auto-upload the file for you then you can get details about it like this: Code: $full_path = $this->upload_data['full_path']; Add this to the controller uploading the file Code: /* Add this to your HTML form Code: <!-- For image upload --> And finally, add this rule to your config file or to right before you check the form. Code: $this->form_validation->set_rules('valid_image', 'image', 'callback_valid_image'); This form only supports the uploading of ONE image but could be changed to support many if you needed.
[eluser]xwero[/eluser]
I wouldn't put the do_upload in the validation function, it would be the same as storing each post value after it passes the validation. Setting class variables, _upload_dir in this case, are better done in a method because if the class variable changes you can change this in method and none of you code needs to change.
[eluser]Xeoncross[/eluser]
True, it is better if you move the do_upload() function call to the actual method calling the validation - but I have it like this because it is "plug and play" for someone to just paste into there controller. Also, you should not set new variables from methods - they should all be defined in the class so that people working on this class don't get weird results when they happen to create a variable named the same thing. Doing it this way makes sure that everyone knows what variables are in effect just by looking at the constructor or class. Especially since CI passes these values into libraries and models it loads! |
Welcome Guest, Not a member yet? Register Sign In |