I am using a PHP script to change my download link to "http://mydomain.com/get.php?download/" however I am looking to further change this to only show "/get.php?download/ or something more specific like "?php" on my web page where the button holds the link.
Here is my code:
<?php
$path = 'folder/blank.file/';
$mm_type="application/octet-stream";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); //output
exit();
?>
How is this possible? Thanks for your help!
Edit: Am using lighttpd and invision power board.
i dont know if you already have made this (its an old question )...
but i have something that may help.
to rewrite, in IPB you can change the core files ... take a look in here:
{forum-root}/admin/applications_addon/ips/downloads/extensions/furlTemplates.php
in "idmdodownload" take a look in this row:
'out' => array( '/app=downloads(&|&)module=display(&|&)section=download(&|&)do=do_download(&|&)id=(.+?)(&|$)/i', 'files/download/$5-#{__title__}/$6' ),
you can rewrite that and i think you can get what you want ...
take a look at ->my post about rewriting in IPB
<?php
$file = $_GET['name'];
$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
?>
This is a snippet of code in file.php. I am referring to the file using:
File 1
The intent is that on click of the link, ./curr/First File.pdf should download. I do get a download, but on inspecting, it's the webpage with the pdf embedded in the file. Could anyone assist?
If you want to have just the PDF loaded, the above code is all code to be executed.
Drop all surrounding menus, header or footers. Make sure, that no HTML or any other output besides the PDF from readfile() remains, when calling this link.
Try to change the content type to :
header("Content-Type: application/force-download");
I am dynamically generating image in php. The image has a fixed name. I wants a button or hyperlink and onclick of that button, users should be able to export image rather than right click and save as image options. The problem is that in case of excel,pdf or doc files, I can specify the path of file and browser automatically asks for the open or save option but for images, it opens them in separate window.I want same dialog box for saving the image as for the other files like excel,pdf.Please help me on this.
Thanks
you can set the header to force download (for PHP):
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: image/png");
Then you can read the file
readfile($file);
What you can do is force image download using .htaccess if you are using Apache or directly via php using header tags
PHP Example
$file = "PATH TO FILE" ;
$fileName = basename($file);
$fileSize = filesize($file);
set_time_limit(0);
header("Pragma: public");
header("Expires: 0"); // Cache Options
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\" " . $fileName ."\"");
header("Content-length: $fileSize");
readfile($file);
I would to know the command in a PHP script to get in output and save a file from my site.
Thanks
See here for a good description of how to force the output of a php script to be a download.
The basics of it are:
// Set headers
header("Cache-Control: public");
header("Content-Description: File Download");
header("Content-Disposition: attachment; filename=" + $filename);
header("Content-Type: application/zip"); // or whatever the mime-type is
// for the file you want to download
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($full_path_to_file);
As an addition (provided by Gordon's comment), see the 1st example on the php documentation here
At the End of the files or used in clicking files, you can add this
$filesh = "check.xls";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filesh));
header("Content-Description: File Transfer");
readfile($filesh);
if you got any header using problem means, top of the file you can add ob_start(); function
If you mean getting output, contents from other site or location, this what you need file_get_contents
I'm struggling with an odd error. I have a simple web app that grabs stuff from a DB then outputs it as a downloadable csv file. It works on firefox and chrome, but IE fails to recognize it as a csv file (thinking it is a html fle) and when I click save I get the error, "Unable to download {name of file} from {name of site}. Unable to open this internet site. ..."
Code:
session_start();
//some logic goes here...
//generate csv header
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exportevent.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "Event: " . $event_title . "\n";
//print the column names
echo "Last Name, First Name, Company \n";
while($row = mysql_fetch_assoc($result))
{
echo $row['atlname'] . ',' . $row['atfname'] . ',' . $row['atcompany'] . "\n";
}
I've played around with the content-type a whole bunch, but that had no effect.
Update: I've tried text/csv, application/vnd.ms-excel (and variations of this), text/plain, and some others that I now forget with no luck.
This is IE8 btw.
Update 2: The connection is over SSL.
Don't we love IE? :)
Try using those headers:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
header("Content-Transfer-Encoding: binary");
I think that the octet-stream content type forces IE to download the file.
We recently ran into this problem ourselves. See this MSKB article
These are the headers we ended up having to use to get it to work over SSL.
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file_name\";");
header("Content-length: " . strlen($csv_string));
I've had success with the following:
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=File.csv");
Setting the type to application/vnd.ms-excel seemed to do the trick in my case. This is all in a file that is opened by submitting a form using
target="_blank"
The only extra code I had to add for IE to work with SSL was: header("Pragma: public");
So my headers look like this now:
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
We have just had the same issue and after adding many headers and getting a working link I then removed them one by one and found the key one for us was
"Cache-Control: public"
so in the end we just had
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
which worked fine.
Try setting your content type to text/csv instead of application/octet-stream.
Since application/octet-stream is a generic binary mime type (and doesn't match the '.csv' extension), Internet explorer might be ignoring it and computing the mime type based on the file extension.
After using Javascript it will solve your problem.
Use this for IE,
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
For more information i have written tutorial on that,
see - Download JSON data in CSV format Cross Browser Support
Hope this will be helpful for you.
The solution for me was:
header_remove();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=brokerlist.csv');
echo $content;
Did you try the Content-type: text/csv ?
Some time ago I've got a problem with IE6 opening pdf files, and crashing when AdobeReader 6.0 was installed and tried to open file in browser window. Than I found somewhere this header:
header('Content-Type: application/force-download');
And it solved the problem, every pdf file was downloaded and opened in Adobe instead of IE.
This simply doesn't make sense. I tried the accepted answer, all the other answers in here, and it didn't work for me. I tried their permutations, and somehow I managed to make it work in IE like so:
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/vnd.ms-exce");
header("Content-Disposition: attachment; filename=coupons.csv" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($csv));
echo $csv;
die();
One thing I did is to empty the cache every freaking time I test the code. And it still doesn't make sense. Just in case someone might need this desperately ;)
If you are trying to accomplish this task (getting a CSV file to download in IE8) using Salesforce.com (in which case your front-end is Visualforce and you can't set all of the headers, only some of them), here's what you need:
<apex:page cache="true"
contentType="application/octet-stream#myAwesomeFileName.csv"
showHeader="false" sidebar="false" standardStylesheets="false">
<apex:outputText value="{!csvContent}" escape="false"/>
</apex:page>
The key pieces here are cache=true, which, in conjunction with the default expires=0 attribute, achieves the following headers:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
And then the contentType of application/octet-stream --- doing text/csv fails for IE8.