1

I am using csv_helper.php file in helpers for exporting. It is grabing the results from mysql but showing the results only instead of downloading ! Here's the csv_helper

<?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: attachment; filename="' . $download . '"');
        }       
        ob_start();
        $f = fopen('php://output', 'w') 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();
        ob_end_clean();

        if ($download == "")
        {
            return $str;    
        }
        else
        {   
            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;
        }

        echo array_to_csv($array, $download);
    }
}

And here's the controller function:

public function exportUser() {
    $this->load->database();
    $query = $this->db->get('user');
    $this->load->helper('csv');
    query_to_csv($query, TRUE, 'toto.csv');
}

And in the view page it is showing the results: user_id,user_name,user_email,user_pass,user_phone,user_country,user_city,user_zip,user_address,user_type,user_status 53,abcdef,[email protected],12,1,,0,,,Student,1 54,aws,[email protected],12,12,Afghanistan,Kapisa,,,"Resource Person",0 55,onti,[email protected],12,12,,0,,,"Registered User",1 56,edf,[email protected],12,12,Albania,Bulqize,,dewde,"Admin User",1 58,meena,[email protected],,,,,,,"Registered User",0 61,nisat,[email protected],,,,,,,"Registered User",0

but not downloading ! Tried Chrome and mozilla both....

What to do???

Thank you in advance !

4
  • 1
    I believe it should be Content-Disposition: attachment, not attachement. Commented Dec 5, 2014 at 18:45
  • Edited ... but still same case :( Commented Dec 5, 2014 at 18:47
  • Can you verify the headers are being set correctly? The "Network" inspector in most browsers shows the request and response headers. Commented Dec 5, 2014 at 18:51
  • tried this : header('Content-Type: text/csv; charset=utf-8'); but no luck ! Commented Dec 5, 2014 at 18:59

1 Answer 1

1

Try modifying the headers in array_to_csv() funtion:

// Disable caching
$time = gmdate('D, d M Y H:i:s');
header('Expires: Tue, 03 Jul 2001 06:00:00 GMT');
header('Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate');
header('Last-Modified: ' . $time . ' GMT');

// Force download
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');

// Set encoding
header('Content-Disposition: attachment;filename=' . $download);
header('Content-Transfer-Encoding: binary');

Then after the output section, add an exit:

if ($download == "")
{
    return $str;    
}
else
{   
    echo $str;
}
exit;

Or try using CodeIgniter's built-in functions:

public function exportUser() {
    // Load database and query
    $this->load->database();
    $query = $this->db->get('user');

    // Load database utility class
    $this->load->dbutil();
    // Create CSV output
    $data = $this->dbutil->csv_from_result($query);

    // Load download helper
    $this->load->helper('download');
    // Stream download
    force_download('toto.csv', $data);
}

Thanks,

Andrew

Sign up to request clarification or add additional context in comments.

8 Comments

I've modified my answer to include an option for modifying your csv_helper file.
i changed csv_helper.php same case !!
I've edited to add an exit. If that still doesn't work, try turning on display errors to make sure that nothing's interfering with the headers. Even a PHP warning/notice could cause issues.
displaying error in index.php but there is no error !
I have tried with normal text files also. It seems any kind of file is not downloading ! is there something to enable or disable ?? Please help !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.