views:

61

answers:

2

Hi,

I am using Codeigniter and I am trying to upload multiple files into a database. The files are seperate fields not multiple files for one field.

I followed this post -http://codeigniter.com/forums/viewthread/110130/P0/

I almost have it working but I am having some trouble with adding the uploaded data into my db.

Here is my Controller function...

function addStation(){
        // set common properties
        $data['title'] = 'Add new station';
        $data['action'] = site_url('admin/station/addStation');
        $data['link_back'] = anchor('admin/station/index/','Back to list of stations',array('class'=>'back'));

        // set validation properties
        $this->_set_fields();
        $this->_set_rules();

        $config['allowed_types'] = 'pdf|xls|doc|docx';
    $config['max_size'] = '1000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $path_to_uploads='./uploads/files/stations';
    $config['upload_path'] = $path_to_uploads;

    // File specific (overrule global and optional)
$config['logo_file']['upload_path']   = './uploads/images/stations';
$config['logo_file']['allowed_types'] = 'gif|jpg|png';


    $this->load->library('upload', $config);
    //add this
    $this->upload->initialize($config);
if (!$this->upload->do_upload(array('rajar_file','playlist_file','map_file', 'logo_file'))){
        $error = $this->upload->display_errors();
        echo "<script>alert($error);</script>";
    }else{
        $data = array('upload_data' => $this->upload->data(array('rajar_file','playlist_file','map_file', 'logo_file')));

        $rajar_file_name = $upload_data['rajar_file']['file_name'];
        $rajar_full_file_path = $path_to_uploads.'/'.$rajar_file_name;

         $playlist_file_name = $upload_data['playlist_file']['file_name'];
        $playlist_full_file_path = $path_to_uploads.'/'.$playlist_file_name;

         $map_file_name = $upload_data['map_file']['file_name'];
        $map_full_file_path = $path_to_uploads.'/'.$map_file_name;

         $logo_file_name = $upload_data['logo_file']['file_name'];
        $logo_full_file_path = $path_to_uploads.'/'.$logo_file_name;
    }

// run validation
        if ($this->validation->run() == FALSE){
            $data['message'] = '';
        }else{
            // save data
            $station = array('name' => $this->input->post('name'),
                            'rajar' => $rajar_full_file_path,
                            'playlist' => $playlist_full_file_path,
                            'map' => $map_full_file_path,
                            'logo' => $logo_full_file_path    );
            $id = $this->stationModel->save($station);

            // set form input name="id"
            $this->validation->id = $id;

            // set user message
            $data['message'] = '<div class="success">add new station success</div>';
        }

          $this->template->set('title', 'Admin - Add New Station!');
      $this->template->load('admin/template', 'admin/stationEdit', $data);


    }

After the form is submitted I get the following errors...

Message: Undefined variable: rajar_full_file_path

Message: Undefined variable: playlist_full_file_path

Message: Undefined variable: map_full_file_path

Message: Undefined variable: logo_full_file_path

Am I being daft, I can't figure it out?

Thanks Dan

+1  A: 

I seems like the file upload is failing therefore those variables aren't being set. The method of displaying the upload errors may not be working. Try instead to send the error back to the view:

instead of:

    $error = $this->upload->display_errors();
    echo "<script>alert($error);</script>";

try:

    $error = $this->upload->display_errors();
    $data['message'] = $error;
stormdrain
+1  A: 

Seems that your code is entering the first if statement, then passing the form validation (thus never defining your variables) and then trying to use those variables in the second else code block.

Eric LaForce