Jump to content

[SOLVED] Download problem


Tomski

Recommended Posts

Hi,

 

I'm having problems downloading an MySQL table to a csv file.  I followed this pretty useful tutorial:

 

http://www.ineedtutorials.com/code/php/export-mysql-data-to-csv-php-tutorial

 

But as opposed to getting a download box the output is dumper into the browser.  I assume it's something to do with my header arguments, but having sat here for ages trying a variety of arrangements and getting nowhere I thought I'd ask you guys.  I'm getting the same thing on both Mozilla/5.0 and IE7.  Here is my code:

 

function exportMysqlToCsv($table,$filename){ // = 'export.csv'){

    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = "select * from $table";

    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);


    $schema_insert = '';

    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {

                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= '';
            }

            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for

        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while


    //Letting browser know length
    header("Content-Length: " . strlen($out));

    // Output to browser with appropriate mime type, you choose 
    //header("Content-type: text/x-csv");
    //header("Content-type: text/csv");


    //cache stuff
    header("Expires: 0");
    header("Cache-control: private");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    //header("Content-Type: application/vnd.ms-excel");
    //header("Content-type: text/plain");
    header("Content-type: text/x-csv");
    header("Content-disposition: attachment; filename=" . $filename . ".csv");

    echo $out;
    echo headers_sent(); //testing headers
    exit;

}

?>

 

Cheers,

 

T

Link to comment
Share on other sites

I don't think there is a problem with your code your browser just recognizes the extension and instead of downloading it display's it. The same thing happens when you download a pdf file instead of downloading, the browser reads the file, loads the plugin and display's the pdf in browser.

 

Try adding a different extension and see if that helps

 

P.S. remove the echo headers_sent();

Link to comment
Share on other sites

Cheers for the reply.

 

I thought that was the point of the:

 

header("Content-disposition: attachment; filename=" . $filename);

 

So that you could save the out put as a file instead of displaying it.  I tried it with a pdf extension and get the same thing.

 

T

Link to comment
Share on other sites

I was about to post what's written below.  Then I realised that my connectDB has an echo in if the connection fails.  Removed it and hey presto.  What a tit, I read that header sticky too before posting.

 

Anyway cheers guys.

 

 

My calling code is below.  I'm just trying to get the download to work before I add it to my page so there in no output.  I searched around quite a lot before posting this as well so was aware of that common mistake so I don't think it's that.

 

<?

     include('connectDB.php');  //connect to database

     require 'exportcsv.inc.php';
     $table="arrivals"; // this is the tablename that you want to export to csv from mysql.
     exportMysqlToCsv($table, 'export.csv');

?>


Link to comment
Share on other sites

Add the following two lines immediately after your first opening <?php tag (and please use full <?php tags instead of the short <? tag, which may in fact be the cause of your problem) -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

You may need to temporarily comment out the header() statements to see exactly what is being sent to the browser.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.