PHP - Read-Only spreadsheet filetype?
I'm using a simple web-based PHP application that outputs a table as a spreadsheet
header("Content-Disposition: attachment; filename=" . $filename . ".xls"); header("Content-Type: application/vnd.ms-excel"); //inserts tab delimited text
But I'm finding the downloaded spreadsheet opens as a read-only file and must be saved locally and (in Excel on Windows) the type changed to XLS (from HTML). Is there a way to set the attribute of filetype correctly so that doing a simple save doesn't require correcting the filetype?
Is the file downloaded read-only by nature of security or is this not normal?
Also I don't like the automatic borders created when opening the spreadsheet in Excel or OpenOffice (on Linux). I would prefer to have no border formatting. Is there a way in the file to specify no added formatting or is this built into those applications?
I don't know which version of Excel you're talking about, so I'll suppose that you're using a 2007 or newer version. The extension change problem probably depends on an Office feature called "Extension Hardening"; as far as I know, the only solution is to generate a real Excel file (for example by using the PHPExcel set of classes), and not an HTML file. The downloaded files are read-only for security reasons, since they are being opened in the so called "Protected View":
Files from the Internet and from other potentially unsafe locations can contain viruses, worms, or other kinds of malware, which can harm your computer. To help protect your computer, files from these potentially unsafe locations are opened in Protected View. By using Protected View, you can read a file and inspect its contents while reducing the risks that can occur.
Finally, a word about borders and formatting: with the old Excel 2000 version, you could format the output by simply adding some XML tags in the header section of the HTML code; see the "Microsoft Office HTML and XML Reference" for further details and examples, but keep in mind that it's quite obsolete, so I don't think that this technique still works with the more recent Excel versions. If you want to have more control over the generated ouput, you should not use simple HTML for creating the spreadsheet file. On this post you can also find some alternatives to PHPExcel for writing Excel files.
AFAIK, at least on Windows, it depends on what the user does with the prompt shown by the browser when they follow the link.
If the user chooses to save the file, it won't be read-only. If the user chooses to open it, the file will be saved to a temporary directory and the browser may remove it when it is closed. I am not sure how this mechanism works, but I am assuming there is a lock involved some place that makes the file read only.
If you want save locally, you could set the attribute content-description:
header('Content-Description: File Transfer'); header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment; filename=" . $filename . ".xls");
If you print the tab delimited text and attribute content-dispositionlike xls or csv, Excel application (or other program, like gnumeric or openoffice) recognizes it like xls or csv.