Guest User

Untitled

a guest
Oct 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public function upload_file(){
  2. $csvMimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
  3. if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
  4. if(is_uploaded_file($_FILES['file']['tmp_name'])){
  5.  
  6. //open uploaded csv file with read only mode
  7. $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
  8.  
  9. // skip first line
  10. // if your csv file have no heading, just comment the next line
  11. fgetcsv($csvFile);
  12.  
  13. //parse data from csv file line by line
  14. while(($line = fgetcsv($csvFile)) !== FALSE){
  15. //check whether member already exists in database with same email
  16. $result = $this->db->get_where("tb_person", array("email"=>$line[1]))->result();
  17. if(count($result) > 0){
  18. //update person data
  19. $this->db->update("tb_person", array("name"=>$line[0], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]), array("email"=>$line[1]));
  20. }else{
  21. //insert person data into database
  22. $this->db->insert("tb_person", array("name"=>$line[0], "email"=>$line[1], "phone"=>$line[2], "created"=>$line[3], "status"=>$line[4]));
  23. }
  24. }
  25.  
  26. //close opened csv file
  27. fclose($csvFile);
  28.  
  29. $qstring["status"] = 'Success';
  30. }else{
  31. $qstring["status"] = 'Error';
  32. }
  33. }else{
  34. $qstring["status"] = 'Invalid file';
  35. }
  36. $this->load->view('csvToMySQL',$qstring);
  37. }
  38.  
  39. tb_person
  40. name varchar(100) NOT NULL
  41. email varchar(100) NOT NULL
  42. phone varchar(100) NOT NULL
  43. created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  44. status varchar(100) NOT NULL
  45.  
  46. tb_phone
  47. phone varchar(100) NOT NULL
  48. person_id int(11) NOT NULL
Add Comment
Please, Sign In to add comment