Welcome Guest, Not a member yet? Register   Sign In
File uploading within a form (w/ validation)
#1

[eluser]starenka[/eluser]
Hi, how to utilize upload class within a form using validation class?

<snip>
Code:
public function add()
    {
        $this->load->library('validation');
        /* set rules */
        $rules['name'] = 'trim|required|max_length[255]';
        $rules['comment'] = 'trim|required';
        $rules['dimensions'] = 'trim|required|max_length[20]';
        $rules['price'] = 'trim|required|max_length[5]';
        $rules['photo'] = 'required';
        
        $this->validation->set_rules($rules);

        /* set names */
        $fields['name'] = 'Name';
        $fields['comment'] = 'Comment';
        $fields['dimensions'] = 'Dimensions';
        $fields['price'] = 'Price';
        $fields['photo'] = 'Photo';
        
        $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('', '');
        /* set messages */


        if ($this->validation->run() == false)
        {
            $params = array('title'=>'New item',
            'view'=>'item_add',
            'xtra_css'=>array('src'=>'form','media'=>'screen,projection,tv'));
            $this->load->view('admin/admin_page_skelet_view',$params);
        }
</snip>

if i just use input type file, all i get in $this->validation->photo is a string containing file name and $_FILES is empty.. is there a simple way how to combine validation and upload class?

thx st
#2

[eluser]Yash[/eluser]
Try to write some code where u want to upload the file.You haven't tried it.Just try with some code.
#3

[eluser]starenka[/eluser]
well, obviously if the $_FILES is empty after submitting the form i seem to be in a dead end..

Code:
if ($this->validation->run() == false)
        {
            $params = array('title'=>'New item',
            'view'=>'item_add',
            'xtra_css'=>array('src'=>'form','media'=>'screen,projection,tv'));
            $this->load->view('admin/admin_page_skelet_view',$params);
            #$this->load->view('add_item_view',$params);
        }
        else
        {
            var_dump($this->validation->photo);
            #process uploaded file here
            var_dump($_FILES);
        }
#4

[eluser]Yash[/eluser]
You mean you want to upload file and submit form on the same time.

I don't if it's possible or not. Never tried
#5

[eluser]starenka[/eluser]
bump. any ideas?
#6

[eluser]Colin Williams[/eluser]
After you run validation, if uploading the photo is a requirement for processing the form, try to run the upload process. If that fails, set the error, and serve back the view that has the form. If validation passes and the upload passes, then you can use your model to save the data you need, and serve up a success page.

And for your $_FILES array being empty, you might want to check your PHP config. I've worked on some servers where there was a different global variable to access (or file uploading was disabled altogether). It could also mean that your temporary folder can't be written to, so there's no temp files to hold in the $_FILES array. Try something outside of CI and see if you don't have the same problem.

Also, post your form code. You're not really showing us enough to make a good diagnosis.
#7

[eluser]Nathan Moore[/eluser]
First of all, double check the form in your view. Make sure that the form enctype is set to "multipart/form-data" Or, if you are utilizing the form helper, make sure you are opening your form with form_open_multipart(). If your $_FILES array is empty, verify that all the code is correct in the view.

As for the validation, you can tie in a custom validation method to check to see if a file has been uploaded.

Set your normal validation rules, and then create a custom method for "userfile" that will check to see if $_FILES['userfile']['size'] is greater than 0. If it is, a file has been uploaded and you can return true and proceed with the rest of your process. If not, return false, and the validation class will take care of the rest and serve up your form again.

This is the most efficient way to validate an upload. I hope that helps.
#8

[eluser]starenka[/eluser]
Thx for hints, Colin & Nathan. Will check up on this tonight.
#9

[eluser]starenka[/eluser]
Yep, leaving out the input "file" field out of validation made the trick. $_FILES now contains the uploaded file and that's what i needed... thx
#10

[eluser]sikkle[/eluser]
@ Nathan Moore : hi, just to give trace on the forum of your way to handle if file is uploaded, do you mind to paste the method you use as a callback.

See ya around.




Theme © iAndrew 2016 - Forum software by © MyBB