Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP xlsx header

Tags:

php

header

xlsx

so this works:

myphpfile.php:

<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?> 

that php file is called here and the PDF download works fine:

<a class = "oglasavanje" href = "../../cjenik/myphpfile.php">download</a><br/>

but this doesn't work:

<?php
header('Content-disposition: attachment; filename=myfile.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>

both .pdf and .xlsx files are in the same folder. When I click on the 'download' link the download windows pops up, I save the file, but the file can't be opened. It gives me the following error:

Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

The file opens fine when I open it manually (i.e. without downloading it via the link)

I'm using WAMP, if that's of importance.

Any help is appreciated.

--- EDIT ---

I've found this piece of code on php.net:

ob_clean();
flush();

so the final code looks like this and it works:

$file = "myfile.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush(); 
readfile($file);

Thanks everyone for the answers.

like image 815
GTCrais Avatar asked Apr 17 '12 20:04

GTCrais


People also ask

How to create a phpspreadsheet using xlsx?

Use composer to install PhpSpreadsheet into your project. First, import the needed library and load the Reader of XLSX. Read the excel file using the load () function. Here test.xlsx is the file name. Get the first sheet in the Excel file and convert it to an array using the toArray () function.

What is the use of header in PHP?

header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers. Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

How to export data from PHP to excel using PHP?

Our simple PHP script lets you implement export data to excel functionality. By one click, the user can export data to Excel and download it in a .xls file. Here is the data array ( $data) and we will export these data to Excel using PHP.

How to read and write an Excel file in PHP?

Recently I had a requirement in PHP to read and write an excel file. The first requirement is reading the excel file and push the data to the database. The second requirement is, Generate an Excel file using the data from the database. Excel generation on the server-side. Not the client-side. PHP doesn’t have a built-in excel library.


2 Answers

Depends on your browsers so many things can cause such behavior such as Content-length , Cache-Control etc

Also Change

Content-type to Content-Type

Content-disposition to Content-Disposition

Try

$file = "myfile.xlsx" ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('myfile.xlsx');
like image 156
Baba Avatar answered Oct 02 '22 01:10

Baba


Depending on your web browser, the filename may not be saving fully with periods. If the filename is incomplete, you can try and replace this header:

header('Content-disposition: attachment; filename=myfile.xlsx');

with

$filename = "myfile.xlsx";
header('Content-Disposition: attachment; filename="' . $filename . '"');
like image 38
Scryptronic Avatar answered Oct 01 '22 23:10

Scryptronic