1

I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.

Here is what i tried so far:

if(isset($currency)) {
    header("Content-Type: application/csv");
    header("Content-Disposition: attachment;Filename=Pricelogs.csv");
    ob_clean();
    $filename = "/tmp/".uniqid().".csv";
    exportCSVFile($filename, $currency, $country, $provider);
    readfile($filename);
    //unlink("'".$filename."'");
} else {
    echo "ERR_USERNAME_PASSWORD";
    exit();
}

I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes. I also tried header("Content-Type: text/csv"); Also:

    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=\"".$filename."\";" );
    header("Content-Transfer-Encoding: binary");

5 Answers 5

1

PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.

http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv

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

2 Comments

Yas alternative is good one as for now,as i had tried everything else..Thanks a lot for your help..
@PankajDadure this seems to be not working now. Any other possible way to preview CSV files
1

I usually just do:

header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");

// print CSV lines here

exit();

1 Comment

Actually i also use to work in same manner but know as i mentioned instead of prompting for download,it just simply gets displayed on the browser. Also i tried it on all mazor browser,thinking,there might be issue on just a certain browser. Aslo i added this line in .htaccess file:AddType application/octet-stream csv
0

I can tell you this combination is working for me:

$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;

If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.

1 Comment

:Thanks for your suggestion DannyB but I had already worked on it by dividing into single modules i.e plainly,but all contents just gets displayed on browser only.
0

You might try this.

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );

print $content;

Comments

0

For large files you need to get your output buffer started

add : ob_start(); at the start

ob_clean(); at the end of you file

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.