1

I was trying to upload several files together with CI framework. what I want is upload files to different folders. Therefore, I tried check file input element's name and set fill path.

Here is the code to get clear idea about my problem.

$config['allowed_types'] = 'jpg|doc|pdf|docx';
        //$config['max_size']   = '100';
        //$config['max_width']  = '1024';
        //$config['max_height']  = '768';
            if($this->input->post('bankbook_attachment')){
                echo 'bank'.$this->input->post('bankbook_attachment');
                $config['upload_path'] = './uploads/bank/';

                $fillselect='bankbook_attachment';
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload($fillselect))
                {
                        $error = array('error' => $this->upload->display_errors());

                        echo 'error'.print_r($error);
                }
                else
                {
                     //   $data = array('upload_data' => $this->upload->data());
                       // print_r($data);
                        $datame=$this->upload->data();
                        $fillnamebank= $datame['file_name'];
                        echo 'fillname is'.$fillnamebank;
                      // $this->load->view('upload_success', $data);
                }


            }
            if($this->input->post('emp_nic_attachment')){
                $config['upload_path'] = './uploads/nic/';
                $fillselect='emp_nic_attachment';
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload($fillselect))
                {
                        $error = array('error' => $this->upload->display_errors());

                        echo 'error'.print_r($error);
                }
                else
                {
                     //   $data = array('upload_data' => $this->upload->data());
                       // print_r($data);
                        $datame=$this->upload->data();
                        $fillnamenic= $datame['file_name'];
                        echo 'fillname is'.$fillnamenic;
                      // $this->load->view('upload_success', $data);
                }
            }

but when I run the code I notice that it doesn't validate if conditions, it leaves them. In CI examples I have notice that they don't use post array to get the name but directly hard code. But which is impossible in my application.

How to solve this. I think simply problem is in this line $this->input->post('emp_nic_attachment')

2
  • Why do you need this $this->input->post('emp_nic_attachment') Commented Dec 24, 2012 at 6:03
  • I need to check which fill I am uploading and according to fill name file is sent to different folders to store file I upload. Commented Dec 24, 2012 at 6:06

2 Answers 2

1

Since you are updating the upload directory in the $config, i don't think you have to check the 'fill name'. The below code would be enough

$config['upload_path'] = './uploads/bank/';
$this->load->library('upload', $config);

if ( ! $this->upload->do_upload('bankbook_attachment'))
{
    $error = array('error' => $this->upload->display_errors());
    echo 'error'.print_r($error);
}
else
{
    $datame=$this->upload->data();
    $fillnamebank= $datame['file_name'];
    echo 'fillname is'.$fillnamebank;
}

$config['upload_path'] = './uploads/nic/';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('emp_nic_attachment'))
{
    $error = array('error' => $this->upload->display_errors());
    echo 'error'.print_r($error);
}
else
{
    $datame=$this->upload->data();
    $fillnamenic= $datame['file_name'];
    echo 'fillname is'.$fillnamenic;
}
Sign up to request clarification or add additional context in comments.

5 Comments

but I am wondring why I can not get $this->input->post('bankbook_attachment')
and when I run above code I get the error : `The upload path does not appear to be valid.'. I think that's repeating same variable in the same scope. that's why I evalute in a if condition
The uploaded data is found in $_FILES not in $_POST. I think thats why $this->input->post('bankbook_attachment') is not returning anything. May be there is a $this->input->file() function in CI, not sure about that
have you checked it with one file element. ie without the upload_path overwriting. This will confirm if the error is bcoz of the upload_path repeating
@kiriappa it seems there is a initialize() for the upload object. Please check this thread . There are some suggestions to upload files to multiple directories. Seems like an old post.
0

After spending several hours and forums help, I could find the answere. simple and important concept is we have to load upload library and config file each time when we do multiple uploading.

$config['upload_path'] = './uploads/bank/';
                $config['allowed_types'] = 'doc|docx|pdf|jpg';
        $fillselect='bankbook_attachment';
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($fillselect))
        {
            $error = array('error' => $this->upload->display_errors());

            echo 'error'.print_r($error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
                       // print_r($data);
                        $datame=$this->upload->data();
                        $fillnamebank= $datame['file_name'];
            echo 'fillname is'.$fillnamebank;
                      // $this->load->view('upload_success', $data);
        }

                $config['upload_path'] = './uploads/nic/';
                $config['allowed_types'] = 'doc|docx|pdf|jpg';
        $fillselect2='emp_nic_attachment';
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($fillselect2))
        {
            $error = array('error' => $this->upload->display_errors());

            echo 'error'.print_r($error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
                       // print_r($data);
                        $datame=$this->upload->data();
                        $fillnamenic= $datame['file_name'];
            echo 'fillname is'.$fillnamenic;
                      // $this->load->view('upload_success', $data);
        }

another mistake I did was calling uplod library in constructor. It is uselss.

Comments

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.