1

In codeigniter i have a form which contains some text and file(input type=file) fields. Some text fields are required. When i fill the form with file but missed one required field and submit the form. All fields are again repopulate the text other than file field . How can i get the selected files when form submission failed.

I tried set_value() but it seems not work.

and one more thing i can upload multiple files at a time:

$data = array ( 'name'=>'data_sheet[]',
                        'id'=>'data_sheet',
                        'class'=>'form-control',
                        'value'=>set_value('data_sheet', $data_sheet), 
                        'onchange'=>'validateFileInput(this)',
                      );
                    echo form_upload($data,'','multiple');

Here is the validation :

$this->form_validation->set_rules('name','name' ,'trim|required');
    $this->form_validation->set_rules('data_sheet','data sheet','trim');
if ($this->form_validation->run() == false)
    {
        $this->template->write_view('content', 'user/user_form',$data,TRUE);
    }

When i set the set_value like :

set_value=json_encode($_FILES)

<input type="file" name="data_sheet[]" value="{"data_sheet":{"name":["download.jpg"],"type":["image\/jpeg"],"tmp_name":["\/tmp\/phpTxQzNe"],"error":[0],"size":[4792]}}" id="data_sheet" class="form-control" onchange="validateFileInput(this)" multiple="">

How can i use thos to upload the file

4
  • show us the validation code. Commented Oct 23, 2015 at 9:14
  • @NiranjanNRaju see the updated code Commented Oct 23, 2015 at 9:16
  • show the form_validation->run() == false) part Commented Oct 23, 2015 at 9:17
  • @NiranjanNRaju check it... Commented Oct 23, 2015 at 9:22

1 Answer 1

2

Modify your code to be like this:

      $data = array ( 'name'=>'data_sheet[]',
                    'id'=>'data_sheet',
                    'class'=>'form-control',
                    'value'=>$_FILES['data_sheet[]']['tmp_name'],
                    'onchange'=>'validateFileInput(this)',
                  );
                echo form_upload($data,'','multiple');
Sign up to request clarification or add additional context in comments.

8 Comments

thank you for your answer it shows error like array to string conversion
Message: Undefined index: data_sheet[]
What do you set for name of input file?
check my question it is like data_sheet[]
For multiple input file, I suggest you go to this url: stackoverflow.com/questions/15941592/php-multiple-file-array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.