title: Codeigniter Uploading excel and Saving to database (Network Error (tcp_error))
body: <p>Really need your support on this one.
  This works on intranet not until we decided to upload it on public.</p>
  
  <p>Im using Codeigniter + SQL Server + Apache</p>
  
  <p>This how it should work:
  upload the excel file, read the content, save to database and send an email &amp; sms.</p>
  
  <p>But:
  Everytime I upload the excel file after 3 Mins it stops and got this error written on Developer Opt->Network Tab:
  "Network Error (tcp_error)
  A communication error occurred: ""
  The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time."
  <a href="https://i.stack.imgur.com/ms0CH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ms0CH.png" alt="enter image description here"></a></p>
  
  <p>Here's my code:</p>
  
  <pre><code>    public function do_upload()
      {
          set_time_limit(0);
          ignore_user_abort(1);
          $accID = $this-&gt;input-&gt;get('acc');
          $rmi = $this-&gt;input-&gt;get('rmi');
          $spInst = $this-&gt;input-&gt;get('spInst');
          $dateReq = '';
          $dateReqq = $this-&gt;input-&gt;get('dateReq');
          if($dateReqq==null or $dateReqq==''){
              $dateReq = date('Y-m-d');
          }else{
              $dateReq = $this-&gt;input-&gt;get('dateReq');
          }
  
          $dateNow = date("F_d_Y__h_i_s__A");
          $status = "";
          $msg = "";
          $file_element_name = 'file';
           $file_name = '';
          if ($status != "error")
          {
              $config['upload_path'] = 'c:/xampp/htdocs/eDR/assets/uploads/';
              $config['allowed_types'] = 'xlsx';
              $config['max_size'] = 60000;
              $config['overwrite']            = TRUE;
              $config['remove_spaces']        = TRUE;
              $config['file_name']            = uniqid('file')."_". $dateNow;
              $this-&gt;load-&gt;library('upload', $config);
  
              if (!$this-&gt;upload-&gt;do_upload($file_element_name))
              {
                  $status = FALSE;
                  $msg = $this-&gt;upload-&gt;display_errors('', '');
              }
              else
              {
                  $data = $this-&gt;upload-&gt;data();
                  //$file_id = $this-&gt;files_model-&gt;insert_file($data['file_name'], $_POST['title']);
                  //if($file_id)
                  //{
                  $file_name = $data['file_name'];
                  $file = "c:/xampp/htdocs/eDR/assets/uploads/" . $file_name;
                  $this-&gt;do_read($file,$accID,$rmi,$spInst,$dateReq);
                  $status = TRUE;
                  $msg = "File successfully uploaded";
                  //}
                  //else
                 // {
                   //   unlink($data['full_path']);
                    //  $status = "error";
                     // $msg = "Something went wrong when saving the file, please try again.";
                 // }
              }
              @unlink($_FILES[$file_element_name]);
          }
  
  
  
  
        echo json_encode(array('status' =&gt; $status, 'msg' =&gt; $msg, 'name' =&gt; $file_name));
      }
  
  
      public function do_read($file,$accID,$rmi,$spInst,$dateReq){
          //load the excel library
          $this-&gt;load-&gt;library('excel');
          //read file from path
          $objPHPExcel = PHPExcel_IOFactory::load($file);
          //get only the Cell Collection
          $cell_collection = $objPHPExcel-&gt;getActiveSheet()-&gt;getCellCollection();
          //extract to a PHP readable array format
          foreach ($cell_collection as $cell) {
              $column = $objPHPExcel-&gt;getActiveSheet()-&gt;getCell($cell)-&gt;getColumn();
              $row = $objPHPExcel-&gt;getActiveSheet()-&gt;getCell($cell)-&gt;getRow();
              $data_value = $objPHPExcel-&gt;getActiveSheet()-&gt;getCell($cell)-&gt;getValue();
              //header will/should be in row 1 only. of course this can be modified to suit your need.
              //if ($row == 1) {
                //  $header[$row][$column] = $data_value;
              //} else {
                  $arr_data[$row][$column] = $data_value;
              //}
          }
          //send the data in an array format
          //$data['header'] = $header;
          $drCount = ($this-&gt;up-&gt;get_DRcount($accID));
          $date = date("F d, Y  h:i: s A");
          $data['values'] = $arr_data;
          $last = count($arr_data) - 1;
          $totalQty = 0;
          $abbr = $this-&gt;up-&gt;get_Abbr($accID);
          $TATnAGING = '';
          $dAdd ='';
          $posReq='';
          $reqD;
          foreach ($arr_data as $i =&gt; $row)
          {
              if(     empty($row['B']) or
                      empty($row['C']) or
                      empty($row['D']) or
                      empty($row['E']) or
                      empty($row['F']) or
                      empty($row['G']) or
                      empty($row['H']) or
                      empty($row['I']) or
                      empty($row['J']) or
                      empty($row['K'])
              ){
                      $msg = 'B = ' . $row['B'] . ' C = ' . $row['C'] . ' D = ' . $row['D'] . ' E = ' . $row['E'] . ' F = ' . $row['F'] . ' G = ' . $row['G'] . ' H = ' . $row['H'] . ' I = ' . $row['I'] 
                              . ' J = ' . $row['J'] . ' K = ' . $row['K']  ;
                   $status = FALSE;
                   echo json_encode(array('status' =&gt; $status, 'msg'=&gt;$msg));
                   exit();
              }
          }
          foreach ($arr_data as $i =&gt; $row)
          {
  
              try {
              $reqDatee        = date('F d, Y', strtotime($dateReq));
              $start = new DateTime( $reqDatee );
              $end = new DateTime(date("F d, Y"));
              $oneday = new DateInterval("P1D");
              $days = array();
              $data1 = "7.5";
              foreach(new DatePeriod($start, $oneday, $end-&gt;add($oneday)) as $day) {
                  $day_num = $day-&gt;format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
                  if($day_num &lt; 6) { /* weekday */
                      $days[$day-&gt;format("Y-m-d")] = $data1;
                  } 
              }               
  
              $TATnAGING   = count($days). ' day/s';
  
              $dAdd = trim(strtoupper($row['D']));
              $posReq = trim(strtoupper($row['B']));
              $reqD = $dateReq;
              $isFirst = ($i == 0);
              $isLast = ($i == $last);
              $id = $this-&gt;session-&gt;userdata('username');
              $totalQty += $row['G'];
              } catch (Exception $e) {
                      $msg = 'Caught exception: '.  $e-&gt;getMessage(). "\n";
                      $status = FALSE;
                      echo json_encode(array('status' =&gt; $status, 'error'=&gt;$msg));
                      exit();
              }
              if($i&gt;1){
                  $data1 = array();
  
              try {
  
              if(trim(strtoupper($row['B']))=='YES' or trim(strtoupper($row['B']))=='Y'){
                  $data1 = array(
                      "drRef"             =&gt;      $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
                      "postReq"           =&gt;      trim(strtoupper($row['B'])),
                      "reqDate"           =&gt;      $dateReq,
                      "reqBy"             =&gt;      trim(strtoupper($row['C'])),
                      "deliveryAcc"       =&gt;      trim(strtoupper($row['D'])),
                      "matCode"           =&gt;      trim(strtoupper($row['E'])),
                      "matDescription"    =&gt;      trim(strtoupper($row['F'])),
                      "qty"               =&gt;      $row['G'],
                      "UOM"               =&gt;      trim(strtoupper($row['H'])),
                      "location"          =&gt;      trim(strtoupper($row['I'])),
                      "postRef"           =&gt;      '',
          //          "postDate"          =&gt;      date("F d, Y h:i:s A"),
          //          "postBy"            =&gt;      $this-&gt;session-&gt;userdata['name'],
                      "status"            =&gt;      'PENDING FOR POSTING',
          //          "TAT"               =&gt;      trim(strtoupper($row['O'])),
          //          "AGING"             =&gt;      trim(strtoupper($row['P'])),
                      "userID"            =&gt;      $id,
                      'addedBy'           =&gt;      $this-&gt;session-&gt;userdata('name'),
                      'addedUsing'        =&gt;      gethostbyaddr($_SERVER['REMOTE_ADDR']),
                      'dateAdded'         =&gt;      $date,
                      'seqNo'             =&gt;      uniqid(),
                      'accID'             =&gt;      $accID,
                      'fromIMEI'          =&gt;      trim(strtoupper($row['J'])),
                      'toIMEI'            =&gt;      trim(strtoupper($row['K'])),
                      );  
                  }else{
                      $data1 = array(
                      "drRef"             =&gt;      $abbr .'2017' . sprintf("%07d", ($drCount + 1)),
                      "postReq"           =&gt;      trim(strtoupper($row['B'])),
                      "reqDate"           =&gt;      $dateReq,
                      "reqBy"             =&gt;      trim(strtoupper($row['C'])),
                      "deliveryAcc"       =&gt;      trim(strtoupper($row['D'])),
                      "matCode"           =&gt;      trim(strtoupper($row['E'])),
                      "matDescription"    =&gt;      trim(strtoupper($row['F'])),
                      "qty"               =&gt;      $row['G'],
                      "UOM"               =&gt;      trim(strtoupper($row['H'])),
                      "location"          =&gt;      trim(strtoupper($row['I'])),
                      "postRef"           =&gt;      '',
                      "postDate"          =&gt;      date("F d, Y h:i:s A"),
                      "postBy"            =&gt;      $this-&gt;session-&gt;userdata['name'],
                      "status"            =&gt;      'POSTED',
                      "TAT"               =&gt;      $TATnAGING,
                      "userID"            =&gt;      $id,
                      'addedBy'           =&gt;      $this-&gt;session-&gt;userdata('name'),
                      'addedUsing'        =&gt;      gethostbyaddr($_SERVER['REMOTE_ADDR']),
                      'dateAdded'         =&gt;      $date,
                      'seqNo'             =&gt;      uniqid(),
                      'accID'             =&gt;      $accID,
                      'fromIMEI'          =&gt;      trim(strtoupper($row['J'])),
                      'toIMEI'            =&gt;      trim(strtoupper($row['K'])),
                      );  
              }
  
                  } catch (Exception $e) {
                     $msg = 'Caught exception: '.  $e-&gt;getMessage(). "\n";
                      $status = FALSE;
                      echo json_encode(array('status' =&gt; $status, 'error'=&gt;$msg));
                      exit;
                  }
              $insert = $this-&gt;up-&gt;save($data1);
              $data1 = array();
              }
          }
          if($posReq=='YES' or $posReq=='Y'){
                  $data1 = array(
                      'drRef' =&gt; $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
                      'accID' =&gt; $accID,
                      'userID' =&gt; $this-&gt;session-&gt;userdata['id'],
                  //    'postRef' =&gt; trim(strtoupper($row['K'])),
                  //    'postBy' =&gt; $this-&gt;session-&gt;userdata['name'],
                  //    'postDate' =&gt; $date,
                      'totQty' =&gt; $totalQty,
                      'status' =&gt; 'PENDING FOR POSTING',
                      'isPosted' =&gt; 'No',
                      'isApproved' =&gt; 'Pending',
                      'approverID' =&gt; $this-&gt;session-&gt;userdata('head'),
                      'postReq' =&gt; $posReq,
                      'reqDate' =&gt; $reqD,
                      'deliveryAdd' =&gt; $dAdd,
                      'reason'    =&gt; urldecode($rmi),
                      'spInst'    =&gt; urldecode($spInst),
                  );
              }else{
                  $data1 = array(
                      'drRef' =&gt; $abbr . '2017' . sprintf("%07d", ($drCount + 1)),
                      'accID' =&gt; $accID,
                      'userID' =&gt; $this-&gt;session-&gt;userdata['id'],
                      //'postRef' =&gt; $this-&gt;session-&gt;userdata('username'),
                      'postBy' =&gt; $this-&gt;session-&gt;userdata['name'],
                      'postDate' =&gt; $date,
                      'totQty' =&gt; $totalQty,
                      'status' =&gt; 'POSTED',
                      'isPosted' =&gt; 'Yes',
                      'isApproved' =&gt; 'Pending',
                      'approverID' =&gt; $this-&gt;session-&gt;userdata('head'),
                      'postReq' =&gt; $posReq,
                      'tat' =&gt; $TATnAGING  ,
                      'reqDate' =&gt; $reqD,
                      'deliveryAdd' =&gt; $dAdd,
                      'reason'    =&gt; urldecode($rmi),
                      'spInst'    =&gt;  urldecode($spInst),
                  );
              }
  
          $insert = $this-&gt;up-&gt;saveOrder($data1);
  
          $drRef = $abbr . '2017' . sprintf("%07d", ($drCount + 1));
  
          $id = $this-&gt;up-&gt;get_Data('head','tblUser','userID',$this-&gt;session-&gt;userdata('id'));
          $recepient = $this-&gt;up-&gt;get_Data('email','tblUser','userID',$id);
          $name = $this-&gt;up-&gt;get_Data('name','tblUser','userID',$id);
  
          $config = Array(
               'protocol' =&gt; 'smtp',
               'smtp_host' =&gt; '182.50.151.61',
               'smtp_port' =&gt; 587,
               'smtp_user' =&gt; 'user',
               'smtp_pass' =&gt; 'pass',
               'mailtype'  =&gt; 'html',
               'charset'   =&gt; 'iso-8859-1'
               );
               $this-&gt;load-&gt;library('email', $config);
               $this-&gt;email-&gt;set_newline("\r\n");
              // Set to, from, message, etc.
           $this-&gt;email-&gt;from('eDR@domain.com', 'e - Delivery Receipt'); 
           $this-&gt;email-&gt;to($recepient);
           //$this-&gt;email-&gt;subject('eDR Notifications');
           $this-&gt;email-&gt;subject('eDR Notification &lt; DR Request &gt;');
           $message = 'Message';
           $this-&gt;email-&gt;message($message);
           $result = $this-&gt;email-&gt;send();
           // if (!$result) { 
               // $this-&gt;email-&gt;print_debugger();
           // }
  
          $receiver = $this-&gt;up-&gt;get_Data('phoneNum','tblUser','userID',$this-&gt;session-&gt;userdata('head'));
          $sms = array(
              'receiver'  =&gt;  $receiver,
              'msg'       =&gt;  'message',
              'status'    =&gt;  'Send',
          );
          $this-&gt;up-&gt;sendSMS($sms);
  }
  </code></pre>
  
  <p>Thanks.</p>