Imbi
Imbi

Reputation: 23

PHP file transfer an Excel xls file from the server to a local PC doesn't work

I have an Excel 2013 xls file on my server and I would like it to download to my local Windows download directory.

$filename = 'test.xls';

$file_path = $filename;

if (file_exists($file_path)) {
    // Header setzen
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_path));

    readfile($file_path);

    exit;
} else {
    echo 'File not found.';
}

The download works just fine but the xls file is not readable. The error message is that Excel can't open the file because the fileformat is incorrect.

Upvotes: 1

Views: 70

Answers (0)

Related Questions