HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /var/www/html/pmw24/driver_settlement/applicationold/helpers/csv_helper.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('array_to_csv'))
{
    function array_to_csv($array, $download = "")
    {
        if ($download != "")
        {    
            header('Content-Type: application/csv');
            header('Content-Disposition: attachement; filename="' . $download . '"');
            //header("Content-Type: application/csv;");
        }        

         ob_start();
       $f = fopen($download, 'wb') or show_error("Can't open php://output");
        $n = 0;   

        foreach ($array as $line)
        {

            $n++;
            if ( ! fputcsv($f, $line))
            {
                show_error("Can't write line $n: $line");
            }
           
        }
        fclose($f) or show_error("Can't close php://output");
        $str = ob_get_contents();
        echo $str;
       //  //ob_end_clean();

       //  if ($download == "")
       //  {
       //      return $str;    
       //  }
       //  else
       //  {    
       //      header('Content-Type: application/csv');
       //       header('Content-Disposition: attachement; filename="' . $download . '"');
       //  }  

        //$header = array("Username","Name","Gender","Email"); 
  // fputcsv($f, $header);
   // foreach ($usersData as $key=>$line){ 
   //   fputcsv($file,$line); 
   // }
  // fclose($f);
   //$str = ob_get_contents();
  // ob_end_clean();
    
    //echo $str;      
    }
}

if ( ! function_exists('query_to_csv'))
{
    function query_to_csv($query, $headers = TRUE, $download = "")
    {
        if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
        {
            show_error('invalid query');
        }
        
        $array = array();
        
        if ($headers)
        {
            $line = array();
            foreach ($query->list_fields() as $name)
            {
                $line[] = $name;
            }
            $array[] = $line;
        }
        
        foreach ($query->result_array() as $row)
        {
            $line = array();
            foreach ($row as $item)
            {
                $line[] = $item;
            }
            $array[] = $line;
        }

        //print_r($array); die;
        echo array_to_csv($array, $download);
    }
}

/* End of file csv_helper.php */
/* Location: ./system/helpers/csv_helper.php */