![]() |
File upload validation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: File upload validation (/showthread.php?tid=16070) |
File upload validation - El Forum - 04-11-2009 [eluser]stef25[/eluser] sorry to post in an old thread, but i dont see how pistolpete's suggestion in post#2 can work. its very simple - the output is always that no file was uploaded. been breaking my head on this the past 4 hours. some official docs on how to handle this would be appreciated by everyone im sure. File upload validation - El Forum - 04-11-2009 [eluser]mindesign[/eluser] Lets see some of your code. one thing that I changed was that I removed "required" from the form validation as this would return that no file was uploaded every time. instead, in the callback function the act of trying to upload the file wil check if a file was selected and if not it returns a false and an error of no file selected. so in my form rules, i only have the call back function for the image upload. like i said post some code and we'll see if we can work this out. thanks! File upload validation - El Forum - 04-11-2009 [eluser]stef25[/eluser] i should be thanking you :-) im retrying with your suggestion of leaving out "required". determined to get this done before the day's end. ill post back shortly File upload validation - El Forum - 04-11-2009 [eluser]stef25[/eluser] ok here we go. these are two function in my controller. whether or not i pick a file for upload, i always get "You did not select a file to upload." im wondering - what do you use as the first parameter inthe set_rules function? the name of the field or 'userfile' (someone mention this was required, but what if you have multiple file upload fields?) or, $_FILES['prod_img'] ? Code: function create() { thanks for any pointers! File upload validation - El Forum - 04-11-2009 [eluser]stef25[/eluser] editing my own posts here. it seems 'userfile' is the way to go as first param for the set_rules function. when using multiple fields, you can use this Code: $field_name = "some_field_name"; this helps alot. again id like to stress that pistolpete's solution in post#2 (what i was basing myself on all afternoon!) does NOT work. File upload validation - El Forum - 04-11-2009 [eluser]mindesign[/eluser] Ah yes you do need to specify if you are not using the default userfile as the file upload id. So did this solve what you were having issues with? File upload validation - El Forum - 04-12-2009 [eluser]stef25[/eluser] ive certainly progressed, thanks! im just wondering now how to get $image_data = $this->upload->data(); back to the create() function from where the _do_upload callback is called. i return $image_data, but its not available in create(). do you have to declare $image_data as private, public, ... ? if so, where? thanks! Code: function _do_upload($file) { File upload validation - El Forum - 04-12-2009 [eluser]pistolPete[/eluser] [quote author="stef25" date="1239492798"] it seems 'userfile' is the way to go as first param for the set_rules function. ... again id like to stress that pistolpete's solution in post#2 (what i was basing myself on all afternoon!) does NOT work.[/quote] This was almost a verbatim copy from one of my projects and it's working fine there. Do you use something like this? Code: $this->form_validation->set_rules('userfile', 'whatever', 'callback__do_upload'); Could you please provide the full controller and view code? [quote author="stef25" date="1239543705"]do you have to declare $image_data as private, public, ... ? if so, where?[/quote] Have a look at this post: forums/viewreply/551725/ I'd use private as this is only controller internal data. Don't return $image_data in your callback function, save it in a class variable. One reason is this: user_guide/libraries/form_validation.html#callbacks Quote:You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data. File upload validation - El Forum - 04-12-2009 [eluser]stef25[/eluser] pistolpete, thanks for your reply. regarding the availability of the area from my callback function back to create() - how do you create a class variable? below is my class Admin print_r($this->_data); inside _do_upload() prints out the array with the image data - that works. strangely enough, the Code: print "test" . print_r($this->_data); ive played around with public $_data, private $_data but this doesnt make a difference. im also not sure if the function __construct() is required. any help would be much appreciated! Code: class Admin extends Controller { File upload validation - El Forum - 04-12-2009 [eluser]pistolPete[/eluser] You access $_data before the validation ran, so $_data is empty. So you can get the values of the uploaded file like this: Code: if($this->form_validation->run() == FALSE) Quote:where does the “1” come from?The "1" is the return value of print_r(): Quote:If you would like to capture the output of print_r(), use the return parameter. If this parameter is set to TRUE, print_r() will return its output, instead of printing it (which it does by default). By the way: How do you repopulate the form data if validation failed? Have a look at the set_value() function: user_guide/libraries/form_validation.html#repopulatingform Using this function you could omit code like that: Code: //send back to the view One additional comment: Leave out the PHP closing tag: user_guide/general/styleguide.html#php_closing_tag |