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
Related
I have a MariaDB database on a site I am making. In this database, I have a bunch of Excel files that are available for download. For simplicity, lets say I have 5 files. The first named 1.xls, the second named 2.xls, and so on.
How can I force a download from the database if a user types the text 4.xls into a form, then clicks a button? Thanks!
To force the download of your file, link to downloadfile.php?filenum=num
This will be the code in downloadfile.php:
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename='.$_GET[filenum].'.xls');
header('Pragma: no-cache'); //if the file change, user will always download the last version
readfile('/path/'.$_GET[filenum].'.xls');
I have a php file which converts the form data to csv format and then it should get downloaded automatically to the user's local download folder.
$time = time();
$filename = 'exceldownloads/myreport_'.$time.'.csv';
$file = fopen($filename,'w');
fputcsv($file,$rowexcel);
The above code works fine and stores the csv file in the specified folder in server. But my requirement is to download it to a local folder. I have seen many solutions to the above problem, but they are working only if we know the local destination folder. However, My requirement is to make it downloadable to the end-user local download's folder (whose download location Im unaware of). Is there anyway to get it downloaded on to the end user system without specifically mentioning the destination path.
You can export the output of your web page as an attachment, which will be shown as a download to the user. You can do this by outputting appropriate headers right before you make any output to the user.
Here's an example, that creates a download of a CSV file called foo.csv:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
After outputting the headers, you just output all of the file's data to the page content.
*Edit: * Here's a working snippet, as requested:
header("Content-type: text/csv");
header("Content-Disposition: attachment;Filename=foo.csv");
echo implode(";", $rowexcel) . "\r\n"; // you should expand this accordingly
alternatively, here is another snippet, based on your code:
$filename = 'myreport'.time().'.csv';
$f = fopen($filename,'w');
fputcsv($f,$rowexcel);
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="'.$filename.'"');
readfile($filename);
If you are not getting any download, make sure that you don't output anything before the header() calls. Also, make sure that you don't have any UTF8 BOM bytes at the beginning of your PHP file, as these can be misinterpreted for output
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
I know I can just upload the file to a folder in my server, save the directory location to the MYSQL database, and create a download link based on that directory location but what happens when you store a file like a pdf or zip archive directly on the MYSQL database? How do you retrieve that data and create a file download link then?
You shouldn't store file data in your database, that's what the file system is for!
However to answer your question you would store the file contents in a BLOB field which contains the binary data, then to download throw the correct headers and echo the field value.
You will have to set some content headers before outputting the data from your database, like:
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;
Full example at http://onlamp.com/pub/a/php/2000/09/15/php_mysql.html?page=3
You can create a php file like "download.php" that put the headers of that file type (and size) and the echo the contents of the file stored.
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.