title: Unable to insert data in CSV file PHP
body: <p>I am in process of inserting data in the desired CSV file from another CSV file. </p>
  
  <p>CSV file is creating fine with out any problem but its is not insert array data in file.</p>
  
  <p>It only inserts header on the first row.</p>
  
  <p>Below is code I am trying:</p>
  
  <pre><code>date_default_timezone_set('America/New_York');
  set_time_limit(0);
  ini_set("memory_limit", -1);
  
  $realPath = realpath( dirname(__FILE__) );
  $path     = $realPath.'/3pltracking/'; 
  $files    = scandir($path);
  $FilePath = $path.$files[2]; 
  $result   = array();
  $date     = date('m-d-Y_his');
  
  if (file_exists($FilePath)) 
  {
      if (($handle = fopen($FilePath, "r")) !== FALSE) 
          {
              $i=0;
              while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) 
              {
                  $i++;
                  if($i==1) continue;
  
                  //$list = array('$data[2],$data[25],$data[4],$data[30],$data[41],$data[27]');
                  echo $data[2].",".$data[25].",".$data[4].",".$data[30].",".$data[41].",".$data[27];
                  echo "&lt;br&gt;";
                  $list = array($data[2].",".$data[25].",".$data[4].",".$data[30].",".$data[41].",".$data[27]);
  
              // the problem is here I believe as it is empty array if I check it outside while loop
              }
  
          fclose($handle);
  
          $headers = array('ReferenceNumber', 'TotalCartons', 'ShipCarrier', 'TrackingNum', 'FreightPP', 'TotalWeight');
  
          $fp = fopen($realPath.'\3pltracking\TrackingFiles\Tracking_File_'.$date.'.csv', 'w');
  
          fputcsv($fp, $headers);
  
          foreach ($list as $line) {
  
              $val = explode(",", $line);
  
              fputcsv($fp, $val);
  
          }
  
          fclose($fp);
  
      } else {
  
          $body = "File Not Found";
  
          }
      }
  </code></pre>
  
  <p>Here is my CSV file data:</p>
  
  <pre><code>TransactionNumber,CustomerName,ReferenceNumber,PurchaseOrderNumber,ShipCarrier,ShipService,ShipBilling,ShipAccount,EarliestShipDate,CancelDate,Notes,ShipToName,ShipToCompany,ShipToAddress1,ShipToAddress2,ShipToCity,ShipToState,ShipToZip,ShipToCountry,ShipToPhone,ShipToFax,ShipToEmail,ShipToCustomerName,ShipToDeptNumber,ShipToVendorID,TotalCartons,TotalPallets,TotalWeight,TotalVolume,BOLNum,TrackingNum,TrailerNum,SealNum,ShipDate,ItemNumber,ItemQuantityOrdered,ItemQuantityShipped,ItemLength,ItemWidth,ItemHeight,ItemWeight,FreightPP,WarehouseID,LotNumber,SerialNumber,ExpirationDate,Supplier,Cost,FulfillInvShippingAndHandling,FulfillInvTax,FulfillInvDiscountCode,FulfillInvDiscountAmount,FulfillInvGiftMessage,SoldToName,SoldToCompany,SoldToAddress1,SoldToAddress2,SoldToCity,SoldToState,SoldToZip,SoldToCountry,SoldToPhone,SoldToFax,SoldToEmail,SoldToCustomerID,SoldToDeptNumber,FulfillInvSalePrice,FulfillInvDiscountPct,FulfillInvDiscountAmt
  242328,PARADIGM TRENDS,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
  33,d,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
  44,PARAdgdfDIGM TRENDS,123810,40-402849,CUSTOMER PICK UP,LTL,FreightCollect,,,,,HG BUYING- JEFFERSON DC 884,HG BUYING- JEFFERSON DC 884,125 LOGISTICS CENTER PKWY,,JEFFERSON,AL,30549,US,,,,,,,30,0,30,0.0174,,,,,,DOV3S,64,64,4,1,1,4,0,1,,,,,,0,0,,0,,,,,,,,,,,,,,,0,0,0
  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,BY3M,176,176,11,1,1,11,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,0
  </code></pre>