CSV File Opening Inline In Browser Rather Than Prompting DL - php

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

Related

PHP Postgtres Generate CSV File for Download

I have a postgresql database table and using PHP for the backend. On the user interface, I provide users with a way of generating reports. What I want to do is that when a user wishes to generate a report, a CSV file should be provided for download.
I already know how to generate a CSV file for results of a query, but now in this case I don't want the file to be saved on disk. Instead, it should be downloaded by the browser.
You simply need to provide a page for download...
#downloadCSV.php?reportID=1
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="yourfile.csv"');
// do your postgresql query and put the result into $csv
echo $csv;
In this way you don't have to create a real file on your server
The second header Content-Disposition: attachment; forces the browser to download a file instead to show the content in a page

Make temporary Files downloadable from Website

On a webservice I'm developing, a user should be able to download his data in a HTML file. This file contains everything (including images as base64).
Now to make the user download this, I would have to create the file and save it on my webserver. Afterwards, I'd have to delete it using a cronjob, because I can't figure out when the download is complete.
Is there another way? Would it be possible to download a file to the user which does not physically exist on my webserver, but gets somehow created temporarily?
Thank you for your help!
As far as the WWW is concerned, there is no such thing as a file. There are just HTTP resources.
The server might get the data from a file, it might not.
Just output the data for the file from your PHP program. (i.e. by putting it outside <?php and ?> or using echo or any other technique that causes PHP to output something).
You need to make sure you use the right Content-Type header, but since you are using HTML, that is text/html which is the default.
You can add a Content-Disposition header if you want the user to be prompted to save their download somewhere instead of rendering the downloaded HTML in the browser window.
header("Content-Disposition: attachment; filename='foo.html'");
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
From: http://php.net/manual/en/function.header.php

PHP force PDF download not working only for browser on Android

I use POST requests to transfer data that is needed to create the downloadable document on the fly.
When the form is submitted, the $_POST array is passed to a function that returns the path of a created FDF file.
The code:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.$file.'"');
passthru("pdftk file.pdf fill_form ".$fdf_file." output - ");
exit;
Work on all browser (Safari, Firefox, Opera, Chrome, Internet Explorer) but not in Android stock browser.
In Android browser, the download taking place but the content of the file being empty or some HTML garbage, or the browser downloading the file but ignoring my file name and trying to save the file under the name of the script that generated it.
Any help is much appreciated.
Thanks!
UPDATE!!!
the solution: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
Try sending the binary encoding header:
header("Content-Transfer-Encoding: binary");
If still not, you may have to send the file under a different file type.
the solution:
Android and the HTTP download file headers
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/

Dynamically Create and Download Doc File

So I'm trying to both dynamically create a .doc file and have the user download it when he clicks a button.
These are the headers i found to download a file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
And these are the headers i found to make a a word document
header('Content-type: application/vnd.ms-word');
header('Content-Disposition: attachment; Filename='.$myFile);
I'm just having a hard time just fitting the picture together because they both tasks have a 'Content-Type' header. Do i create the file first, save it, then download it? Or can i do it all (create a doc file and have user download it) in one php file?
You only need the "headers found to make a word document." The first set are for a generic streaming download.
Your second set of headers are fine. No need for the first. The Content-Disposition header is the one that will typically force a download. (Although, you should be aware that clients can do whatever they want with a file, and you have no direct control over this.)
You can create the file and send it straight to the client without saving it to the server's disk, depending on how you are creating this document.

Download a CSV file straight to the users download folder

Hi I want to have the option on my site for the user to download a CSV file. I have used the code below
<input type="button" value="Download as CSV file" window.location.href='call_log.csv' " />
This does work but when the button is clicked the file is opened in another tab on my browser, What I want to happen is a download straight to the users default download folder
I posted this question before and the response was to include headers ie
Content-Disposition: attachment; filename="call_log.csv"
The page is a php file and if I include headers the page does not load but trys to download the whole page .
Surely I cant put headers in the CSV file , can anyone help me please ?
Thanks
Your server needs to bet set to execute your php script - you're right; there's no need to change that.
What you need to do is send the correct header to the server from your php script. Here's an example from php.net:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
For your csv file, the correct content type is text/csv

Categories