php save recordset to local file - php

I have a list of data compiled from a mysql recordset when I click a button on one of my pages. The data is stored in a variable $list.
It's a site activity log, and the button is a backup button.
Is there any way that I could make it open a SAVE AS dialogue box so I can save that data to a text file on my local comp?

when you click your "back up" button, you should get the user to a new script: this script should take the $list variable from the DB again and format it into a text file, then in order to make it available to the user's browser as a downloadable file, you should use headers (look at http://php.net/manual/en/function.header.php) like this:
<?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');
?>
In this case this is a pdf file (example is from the above link). Changing the content-type to the proper mime-type ("Content-Type: text/plain" for example) and setting the right file name, all that you echo will be sent to the browser as an attached file.
If any question, ask :)

after generate that file just set the physical path of that file and throw header so it will be download at your local system
Cheers

Related

PHP Generate file and promt save as

I have a script for generate a .csv file and then download, but I need the dialog "Save as" for the user because I want a fast replace of the old file.
An easy example, I download the file "myFile.csv", then edit the data and download again for the refresh, but I need to REPLACE the file, and the browser download it as "myFile (1).csv", so I need to change the name. The point here, time is crucial.
I want the dialog "Save as" for the force as the same name and replace it.
A MWE:
<?php
header('Content-Type: text/csv;charset=Windows-1252');
header('Content-Disposition: inline; filename="myFile.csv";');
$delimiter = ',';
$f = fopen('php://output', 'w');
foreach ($cars as $car) {
fputcsv($f, [$car['id'],$car['model'],$car['color'],null], $delimiter);
}
fclose($f);
?>
EDIT: (Aclaration)
Tested on Google Chrome 68
I just realized that I did not mention an important point, when selecting the link for the download, the download automatically starts as myFile (1).csv and that's what I want to stop, I do not want the automatic download, I want the save dialog, save or cancel.
An author can suggest a filename for the file to be saved as (via the URL, the download attribute, or the Content-Disposition response header), but there is no way to make the browser ask the user what filename they want to use.
It is entirely up to the browser if it saves to a default directory or prompts the user for a location to save to.
What you want is impossible.
and the browser download it as "myFile (1).csv"
Because it realizes that there already is a file called myFile.csv in the target folder.
I want the dialog "Save as" for the force as the same name and replace it.
That is not possible.
You can only specify a file name to be suggested to the user when downloading the file, but you can not force anything in this regard.
If the browser is set to automatically save such downloads into a directory without any further user interaction, then there is nothing that can be done about this at all (besides the user changing their settings); If the user has to confirm each download, then they will have to correct the automatically suggested file name myFile (1).csv to just myFile.csv manually before they safe the file, so that they will then get prompted to confirm whether they want to replace the existing file.
Try using this header instead:
Content-Disposition: attachment; filename="myFile.csv"
From: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Content Disposition
The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).
Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"

html/php user download files

On my web server, I have a bat (harmless) file.
And I have code,
Test Bat File
But when the user clicks, it shows the code instead of downloading the file.
You have to right click "save as.." to download the bat file.
Is there way that when a user clicks, it downloads (not having to right click save as)?
Maybe a pop up window that asks user if he/she wants to download the file or not?
you could write a php file, which adds a content-disposition header, sets the mime type to something binary and echo the files content.
Example:
file.php
$batchfile = file_get_contents('batchlocation');
$size = strlen($batchfile);
header('Content-Disposition: attachment; filename="downloaded.bat"');
header('Content-Type: BAT MIME TYPE or something like application/octet-stream');
header('Content-Lenght: '.$size);
echo $batchfile;
You can do so through setting the file in the PHP header() function.
It is explained here:
How to Automatically Start a Download in PHP?

File open/save as dialog and mime types

I am working on a simple document management system for a site - the user can upload around 20 different file types and the docs are renamed then stored in a folder above www, an entry is created in a docs table to capture meta data entered by the user and the item is then retrieved via another php file so the stored location for the files are hidden from the user.
When a user clicks to download a file using a simple a href it calls, for example, "view.php?doc=image.jpg" - when they do this currently the file opens in the browser so a jpg opens a window with pages of "wingdings" like characters etc.
I would like to be able to force a open/save dialogue box so the user decides what to do and my app doesn't try to open in the browser window with the above results.
From a previous posting I found I know I cannot pass the mime type in the "a href" tag so what other options do I have? Could I put header information into the below view.php file, for example?
$_file = $_GET['doc'];
$filename = './dir/'.$_file;
if (file_exists($filename)) {
echo file_get_contents('./dir/'.$_file);
} else {
echo "The file $_file does not exist";
}
;
You could use get_headers() to get the MIME type header of the desired file, and then use header() to output those headers into the file you're showing.
Alternatively, to simply force downloads, this:
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
Should do it.

How to start file download instantly using php headers

well I'm just wondering how I can get an mp3 download to start instantly, as oppose to it simply starting to play in the browser when you directly go to it.
Preferably using php headers.
So essentially when you click the file, I want a download box to appear saving save etc. Right now it just opens and starts playing in the browser.
Thanks
You'll need to create a PHP file that "redirects" to the MP3 file, and point your links to that PHP file.
Code as below:
<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="fileName.mp3"');
readfile('originalFile.mp3');
?>
Note: The line that sets the Content-Disposition header is the critical one.

Is there a way to force the user to download a file from a href link rather than to open it in a browser window?

Basically I wrote a script that generates a xml file based on user input. After the file is generated a download link appears like so:
Download File
But when clicked it opens the xml in the browser, I want it to start downloading when the link it clicked instead. Is there any way to achieve that?
Yeah, there is. It does require specifying some headers. Exactly how it works depends on what language you're using, but here's an example using php, taken off of 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');
?>
Basically, first we tell the client what type of file we're sending, then we tell the client that what we're sending is an attachment, and it's name, instead of it being a page to display, and then finally we print/read the file to the output.
Given that you're already using php to generate the xml file, I would suggest adding the header commands above to the code that generates the xml file, and see if that does the trick.
If you happen to be using Apache for your web server, and you always want to force downloading of XML files, there is a more efficient way to do what #chigley suggested. Just add the following to a .htaccess file.
<Files *.xml>
ForceType application/xml
Header set Content-Disposition attachment
</Files>
What happens when a browser sees a link is not dependent on the link, but rather on the target of the link. Your web server should send the appropriate header: Content-Disposition: attachment;filename="file.xml" to tell the browser that it should prompt to save the file instead of displaying it.
It depends on what the client computer does with XML files. If you doubleclick on a XML file, it will open in your browser probably.
download.php:
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
readfile('/path/to/file.xml');
HTML:
Download

Categories