Bryan
Bryan

Reputation: 1241

Download Created Excel File Without DialoBox on PHP using Header Content

can anyone know on how could I force to download a file without displaying DialogBox (Open/Save). The below code was is my test script to download created excel file but the dialog box appears to download a file.

$filename ="excelreport.xls";
$contents = "testdata1 \t testdata2 \t testdata3 \t \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;

I want to automatically download file and save it on specified directory without dialog box

Upvotes: 0

Views: 1573

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

You cannot do this for security reasons.

Upvotes: 1

Dukeatcoding
Dukeatcoding

Reputation: 1393

You can force to download instead of showing it but I think it is not possible to force the browser in general to download it without a prompt

// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

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

Source: php.net

Upvotes: 2

Related Questions