Jump to content

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.

 

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.