I am using this to put the contents to a file
file_put_contents('abc.txt', $text);
I need to have a pop up for the user after this to save/download the file
how would I do that
This will give the user a download prompt:
<?php
header('Content-type: text/plain');
// What file will be named after downloading
header('Content-Disposition: attachment; filename="abc.txt"');
// File to download
readfile('abc.txt');
?>
The manual on fpassthru() has a complete example.
Use the Content-Disposition header:
header('Content-Disposition: attachment; filename="abc.txt"');
readfile('abc.txt');
Be sure to send the appropriate Content-Type header as well.
You have to pass the right headers:
header("Content-disposition: Atachment");
Related
I use header() to download an xlsx file from given url. The file is downloaded but I can't not open it. It shows error
Below is my code
$url = "http://example.com/attachment/file.xlsx"
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();
Randy, your question looks weird. The URL in the serve response is way different than the one in your code.
Before commencing the download - sending the headers, do a is_file() or other check on the URL and only start the download if the file exists.
I suspect you are trying fopen on URL, not local file and the URL may be either incorrect or on server not allowing fopen on URLs.
Sample:
$url = "http://example.com/attachment/file.xlsx";
if (!fopen($url,'r')) exit('File/URL not accessible');
else fclose($url);
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename=Test.xlsx');
readfile($url);
exit();
Is it possible to let your user download a file with a different name?
For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.
Sure, use a Content-disposition header
header('Content-Disposition: attachment; filename="filetodownload.jpg"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');
Sure you can, just try something like this:
$original_filename = '4324ffsd34.jpg';
$new_filename = 'my_new_filename_is_detailled.jpg';
// headers to send your file
header("Content-Type: application/jpeg");
header("Content-Length: " . filesize($original_filename));
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
// upload the file to the user and quit
readfile($original_filename);
exit;
Hope it helps!
There is also another way if you are using html5
Download
Cheers :3
Nothing wrong with the above but I had to add:
ob_clean();
flush();
before readfile
otherwise I can not open the download jpg/png file
Download
I am creating an xml file on the fly. When a user generates this file I want it to open up a download file dialog with the content that was generated. There is no actual file because it is just generated through php. Any ideas on how to do this?
This is what worked for me. In readfile('newfile.xml'); make sure to give the path of the file correctly. This php page is called from an html page with anchor tag which says - download:
<?php
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('newfile.xml');
?>
source: How do I force the browser to download a file to disk instead of playing or displaying it?
Send a content-disposition attachment header.
header('Content-disposition: attachment; filename=advertise.xml');
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
// if you want to directly download then set expires time
header("Expires: 0");
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');
Is it possible to let your user download a file with a different name?
For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.
Sure, use a Content-disposition header
header('Content-Disposition: attachment; filename="filetodownload.jpg"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');
Sure you can, just try something like this:
$original_filename = '4324ffsd34.jpg';
$new_filename = 'my_new_filename_is_detailled.jpg';
// headers to send your file
header("Content-Type: application/jpeg");
header("Content-Length: " . filesize($original_filename));
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
// upload the file to the user and quit
readfile($original_filename);
exit;
Hope it helps!
There is also another way if you are using html5
Download
Cheers :3
Nothing wrong with the above but I had to add:
ob_clean();
flush();
before readfile
otherwise I can not open the download jpg/png file
Download