Skip to content

Instantly share code, notes, and snippets.

@tbrianjones
Last active October 28, 2021 02:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tbrianjones/a41861c9992659792213 to your computer and use it in GitHub Desktop.
Save tbrianjones/a41861c9992659792213 to your computer and use it in GitHub Desktop.
Simple way to write data to a file and save it to a Client's computer when they click a link, using only PHP and HTML.
<?php
// this is a single page that can access and write any data to a file
// on a clients computer when they click a link using only HTML and PHP
//
// this will keep the data between page loads
session_start();
// download file if $_GET['download_file'] is set to true
if(
isset($_GET['download_file'])
AND $_GET['download_file']
) {
// set headers to tell the browser that you're about to give it a csv file
//
// - you can tell it other types of files too, but most browsers
// will automatically download csvs, rather than try to open them
// - the file will automatically download when the user clicks the link,
// and the filename will be `downloaded_file.csv` ( see header below )
//
// - these must be set before any data is outputted
// - anything that is outputted within this if() statement will be written to the file
//
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="downloaded_file.csv"');
// output data to write to file
//
// - anything that is outputted will be written to the file
// - in this case, we're just creating a .csv with each entry in the
// array written on a new line
//
// - this data can be manipulated and formatted however you'd like
//
echo implode( "\n", $_SESSION['data_for_file'] );
// tada ... the file will download on the client's computer
// retrieve data and store it to session for later download
} else {
// retrieve POST values
//
// - you coiuld retrieve post values from a previous form submission
// and write those to the session to put into the file
//
// connect to database
//
// - you could connect to a database and get data here that could go into the file
//
// data to save to file
//
// - this can be any format and will get stored into a session variable which can contain arrays
//
$data_for_file = array( 'store', 'some', 'data', 'here' );
// store your data, in whatever format, to your session
//
// - you can store it accross lots of vars because you can maniupulate
// it before writing to the file.
//
$_SESSION['data_for_file'] = $data_for_file;
// create a link to download the file
//
// - notice the get parameter that will tell this page to download, rather than display this link
// - you could also send the user to a new page that has the above logic in it
// from within the `if( $_GET['download_file'] ) {` statement.
//
echo '<a href="?download_file=TRUE">Download File</a>';
}
@iholler
Copy link

iholler commented Aug 5, 2019

thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment