Download with smartphone generate xls file with PHPExcel - php

I have a big problem when I want to download on my smart phone a xls file generated by PHPExcel it me download a php file named my page, while on my pc it load properly.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition:inline;filename=fichie.xls');
$writer->save('php://output');
exit();

There's two problems with your line that sets the content disposition.
It should have spaces between the words, which is a standard requirement for headers.
It should be attachment if you want the file to download, as opposed to display inline.
e.g.
header('Content-Disposition: attachment; filename=fichie.xls');

Related

Rename a file with PHP right before it downloads without saving it to the server?

I have an Adobe Illustrator file (AI) that we currently have a link to on a website which then downloads the file to your computer.
The link looks something like this...
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs
What I need to do is rename this file as it downloads.
So I am asking if it is possible to pass this download through another PHP file right before it downloads which would allow me to change the filename on the fly that the user downloads. I cannot change the filename on the server but when it downloads I would like to be able to add some ID numbers to the filename on the fly if this is possibble? Any ideas how to accomplish this without having to resave the image on the server with a new name?
What you are looking for is the Content-Disposition header, as specified in RFC 2183:
Content-Disposition: attachment; filename=example.ai
You can set this header using the PHP header() function.
It's ugly, and assumes these aren't "large" files that would exceed your memory_limit, but
$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;
You could always enhance this to do byte serving - suck 10k from original url, spit out 10k to user, etc...
Just set the Content-Disposition:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
(Example taken from PHP docs: http://www.php.net/manual/en/function.header.php).
Adding id:
$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');

Force download file that's not placed on the server

I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.

CSV File Opening Inline In Browser Rather Than Prompting DL

So I have a php file that creates a csv file with fopen, fputcsv, then fclose and a javascript function for an export button that directs the window to open said csv file. The csv file gets created correctly but when I hit the export button it just opens the csv file in another tab with just the raw data. What I was looking for (and expecting) was it to prompt a download or opening of the file (which I would then open with excel). If I save the page that gets opened now it opens fine in excel but it has the markup from the html (which I don't want) so I'm just trying to see if there is anything in particular that could fix this issue. I have so far tested it in both ie9 and firefox.
Thanks!
Have you sent an appropriate header before sending the output?
ob_clean();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="some_filename_' . date('Ymd') . '.csv"');
Set the content type in the header to application/vnd.ms-excel
header("Content-type: application/vnd.ms-excel");
and then send your file back to the browser (this will force it to open excel). If you want to send the proper content type for a CSV set it to text/csv

Not being prompted to download CSV file

I've created a custom solution in WordPress that will generate a CSV file to be downloaded by clicking a simple hyperlink, linked directly to this file. Instead of being prompted to download the file to the computer; the CSV opens in the the browser window instead.
FWIW I'm on Media Temple using a vanilla install of WordPress.
Send the proper mime type
header('Content-type: text/csv');
And use the Content-Disposition header to tell it to download: http://www.jtricks.com/bits/content_disposition.html
header('Content-Disposition: attachment; filename="mycssfile.csv"');
You always want to send the proper mime type, otherwise firewalls, anti-virus software and some browsers may have issues with it...
You can use PHP's header() function to change Content-type
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="myFile.csv"');
The above code will force a prompt to the user for download. where myFile.csv should be replaced with the path to the file you want downloaded.
This works:
$filename = 'export.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
Also, I personally do not like links on my sites, I like buttons. If you want a button to do for the export function you can use the code below. I just thought I would post it because it took me a bit to figure out the first time :)
<input type="button" value="Export to CSV" onClick="window.location.href='something.php?action=your_action';"/>
You need to send the browser a MIME type of application/csv so it will offload the responsibility of handling the file to whatever the OS recommends (or user chooses).
In PHP (before any output is sent to the client):
header('Content-type: application/csv');

Firefox : Open XLSX file not saving file butn opening binary

I generate a file server side and I want the client to automatically open it : it's a XLSX file. Firefox just opens the file and I see the binary content of the XLSX file in the browser, but I want it to be open via a Save As... box.
It works fine in Chrome with the same code (it saves it) but not firefox...
Any ideas ?
Have a look at this - Php exec and return binary
Are you sending proper headers??
something like
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.xlsx\"");
UPDATE
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=example.xlsx');
header('Pragma: no-cache');
echo file_get_contents("/path/to/yourfile.xlsx");
UPDATE 2
Spread sheet mime types
application/vnd.ms-excel [official]
application/msexcel
application/x-msexcel
application/x-ms-excel
application/vnd.ms-excel
application/x-excel
application/x-dos_ms_excel
application/xls
UPDATE 3
Regarding your javascript problem did you try using
location.href instead of window.open ??
You need to ensure you are sending this mime type as the Content-Type header:-
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
So you need to map the .xslx extension to this mime type on the server

Categories