Jump to content

Downloading CSV file to local PC


Memon

Recommended Posts

I tried with that code, but it didn't work.

Then I searched other sites & made some changes (special line for IE etc), but it still does not work.

 

It gives 6 warnings (for each header line), saying;

Warning: Cannot modify header information - headers already sent by (output started at D:\My Documents\My Webs\test.php:6) in D:\My Documents\My Webs\test.php on line 18

 

Then it gives following warning;

Warning: readfile(stock.csv) [function.readfile]: failed to open stream: No such file or directory in D:\My Documents\My Webs\test.php on line 24

 

Can anybody guide me, where I am mistaking.

 

My script is as follows;

<?php

    $fp = "test.csv";

    echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#0000FF'>File Name : $fp</FONT></P>";

    // make sure it is a file before doing anything!

    if(is_file($fp))

    {

        // required for IE, otherwise Content-disposition is ignored

        if(ini_get('zlib.output_compression'))  ini_set('zlib.output_compression', 'Off');

 

        header("Pragma: public");

        header("Expires: 0");

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

        header("Content-Type: application/force-download");

        header( "Content-Disposition: attachment; filename=".basename($fp));

        header( "Content-Description: File Transfer");

        readfile($file);

 

        echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#00FF00'>SUCCSESS!</FONT></P>";

        exit();

    }

    else {

        echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#FF0000'>ERROR! NO SUCH FILE.</FONT></P>";

    }

 

?>

 

 

Is that making problem?

 

Yes, headers cannot be sent if output has already been sent to the browser, it would cause an error. To generally tests scripts like these I would write data to a "log" file instead, that way you can see the data in the log file and it won't break the script.

OK. I removed all "echo"s.

My Script is now like this.

<?php
    $fp = "test.csv";
    if(is_file($fp))
    {
        // required for IE, otherwise Content-disposition is ignored
        if(ini_get('zlib.output_compression'))  ini_set('zlib.output_compression', 'Off');
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header( "Content-Disposition: attachment; filename=".basename($fp));
        header( "Content-Description: File Transfer");
        readfile($fp);
        exit();
    }
?>

But now it outputs the whole CSV file on screen, instead of downloading it. Still some mistake in my code.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.