Welcome Guest, Not a member yet? Register   Sign In
File upload validation
#21

[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.
#22

[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!
#23

[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
#24

[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() {    
            
            
            //validation
            $this->form_validation->set_rules('prod_name', 'product name', 'trim|required');
            $this->form_validation->set_rules('prod_img', 'product image', 'callback__do_upload');    
            $this->form_validation->set_rules('prod_descr_long', 'long description', 'trim|required');
            $this->form_validation->set_rules('prod_aff_link', 'affiliate link', 'trim|required');
                    
            
            if($this->form_validation->run() == FALSE) {                
                //send back to the view
                $attributes['prod_name'] = $this->input->post('prod_name');
                $attributes['prod_descr_long'] = $this->input->post('prod_descr_long');
                $attributes['prod_aff_link'] = $this->input->post('prod_aff_link');    
            
                $this->load->view('admin_view', $attributes);    
            }
            else {                
                $this->M_Products->createProduct();
                $this->load->view('admin_view', $attributes);
            }        
          }
          
          
          function _do_upload($file) {
              $config['upload_path'] = '/Applications/MAMP/htdocs/my-site/images/_uploads/';
            
            $this->load->library('upload', $config);
                
            if (!$this->upload->do_upload()) {
                //print "one"; exit;
                // set the validation error using the errors provided by the upload class
                $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
                return FALSE;
            }    
            else {
                print "two"; exit;
               print_r($_FILES); exit;
            }
          }

thanks for any pointers!
#25

[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->upload->do_upload($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.
#26

[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?
#27

[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) {
              $config['upload_path'] = '/Applications/MAMP/htdocs/buy-arab-scarves/images/_uploads/';
            $config['allowed_types'] = 'jpg|gif|jpeg|png';
            $config['max_size']    = '2000';
            $this->load->library('upload', $config);
              
            $field_name = "prod_img"; //see http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
            if (!$this->upload->do_upload($field_name)) {
                $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
                return FALSE;
            }    
            else {    
            
                //array with data from uploaded (not processed!) image
                $image_data = $this->upload->data();
            
                //process & resize image
                $config['source_image'] = $config['upload_path'].$_FILES['prod_img']['name'];
                $config['maintain_ratio'] = TRUE;
                $config['height'] = 200;
                $config['create_thumb'] = TRUE;
                
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                
                if($this->image_lib->resize() !== TRUE) {
                    print $this->image_lib->display_errors();
                    exit;
                }
                              
            }
            
            return $image_data;
          }
#28

[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');
I doubt whether that is working because a file upload is saved in the $_FILES array but the input fields which are processed by the form validation class are in the $_POST array.

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.
#29

[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);
in create() prints out "test1". where does the "1" come from?

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 {
        
        public $_data;
    
    function __construct() {
            parent::Controller();
       }
        
        function index() {
        
            ... some code here ...              
        }
            
            
        function create() {                        
            
            print "test" . print_r($this->_data);
            
            $attributes['page_title'] = "Admin";
            $attributes['products'] = $this->M_Products->getAllProductsSidebar();
            $attributes['feedback'] = "";
            $attributes['subtitle'] = "";
            $attributes['form_open'] = array('name' => 'post_new');
            
            //validation
            $this->form_validation->set_rules('prod_name', 'product name', 'trim|required');
            $this->form_validation->set_rules('prod_img', 'product image', 'callback__do_upload');    
            $this->form_validation->set_rules('prod_descr_long', 'long description', 'trim|required');
            $this->form_validation->set_rules('prod_aff_link', 'affiliate link', 'trim|required');    
            
            if($this->form_validation->run() == FALSE) {                
                //send back to the view
                $attributes['prod_name'] = $this->input->post('prod_name');
                $attributes['prod_descr_long'] = $this->input->post('prod_descr_long');
                $attributes['prod_aff_link'] = $this->input->post('prod_aff_link');    
            
                $this->load->view('admin_view', $attributes);    
            }
            else {                
                $this->M_Products->createProduct($attributes);
                $this->load->view('admin_view', $attributes);
            }        
          }          
          
          function _do_upload($file) {
            $config['upload_path'] = '/Applications/MAMP/htdocs/my-site/images/_uploads/';
            $config['allowed_types'] = 'jpg|gif|jpeg|png';
            $config['max_size']    = '2000';
            $this->load->library('upload', $config);
              
            $field_name = "prod_img"; //see http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
            if (!$this->upload->do_upload($field_name)) {
                $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
                return FALSE;
            }    
            else {    
            
                //array with data from uploaded (not processed!) image
                //$image_data = $this->upload->data();
                $this->_data = $this->upload->data();
            
                //process & resize image
                $config['source_image'] = $config['upload_path'].$_FILES['prod_img']['name'];
                $config['maintain_ratio'] = TRUE;
                $config['height'] = 200;
                $config['create_thumb'] = TRUE;
                
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                
                if($this->image_lib->resize() !== TRUE) {
                    print $this->image_lib->display_errors();
                    //exit;
                }
                print_r($this->_data);
                //return TRUE;                          
            }        
          }
?>
#30

[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)
{                
    ....
}
else
{
    print_r($this->_data);
    ...
}

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
$attributes['prod_name'] = $this->input->post('prod_name');
$attributes['prod_descr_long'] = $this->input->post('prod_descr_long');
$attributes['prod_aff_link'] = $this->input->post('prod_aff_link');

One additional comment:
Leave out the PHP closing tag: user_guide/general/styleguide.html#php_closing_tag




Theme © iAndrew 2016 - Forum software by © MyBB