Welcome Guest, Not a member yet? Register   Sign In
CSV upload, once uploaded to a temp folder overwrite the existing database
#46

[eluser]the_unforgiven[/eluser]
This is not working as we discussed previously, for some reason and I have done is create some $vars for the uploaded file.

Code looks like so:
Code:
public function do_upload()
{
  $csv_path = realpath(APPPATH . '/../assets/uploads/CSV/');
  $config['upload_path']   = $csv_path;
  $config['allowed_types'] = '*'; // All types of files allowed
  $config['overwrite']     = true; // Overwrites the existing file
    
  $this->upload->initialize($config);
  $this->load->library('upload', $config);

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

   $this->layout->buffer('content', 'program/upload', $error);
   $this->layout->render();
  }
  else
  {
   //$csv_upload = array('upload_data' => $this->upload->data());

   $image_data = $this->upload->data();
      $fname = $image_data['file_name'];
      $fpath = $image_data['file_path'].$fname;
      $fh = fopen($fpath, "r");


         if (!empty($fh)) {

          $insert_str = 'INSERT INTO wc_program (`JobRef`, `Area`, `Parish`, `AbbrWorkType`, `WorkType`, `Timing`, `TrafficManagement`, `Location`, `Duration`, `Start`, `Finish`)
          VALUES '."\n";

             // Create each set of values.
             while (($csv_row = fgetcsv($fh, 2000, ',')) !== false) {

                 foreach ($csv_row as &$row) {
                     $row = strtr($row, array("'" => "\'", '"' => '\"'));
                 }

                 $insert_str .= '("'
                     // Implode the array and fix pesky apostrophes.
                     .implode('","', $csv_row)
                     .'"),'."\n";
             }

             // Remove the trailing comma.
             $insert_str = rtrim($insert_str, ",\n");

             // Insert all of the values at once.
             $this->db->set($insert_str);
             var_dump($csv_row);
  
             //$this->layout->set('title','Uploaded File Successfully');
             //$this->layout->buffer('content', 'program/success');
    //$this->layout->render();
  
        
         }
        
  
  
  }
}

When doing a var_dump($csv_row) i get: bool(false)
var_dump($fh) shows: resource(89) of type (stream)
var_dump($insert_str) shows all 1700 records from the csv file (obviously too big to post on here)

So i'm guessing the while statement or the whole from IF statement is wrong somewhere. Really really would appreciate some help on this, I need to get it working by tomorrow (monday)

Thanks.


Messages In This Thread
CSV upload, once uploaded to a temp folder overwrite the existing database - by El Forum - 04-07-2013, 10:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB