1

i have a link that allows me to downlowd a text file in my web page but the problem is that i want the user to be able to chose where to save the file i mean when he clicks the link a window should be opened so he can save the file wherever he likes can any one tell me how to do that? thx . here is a part of my code:

$fichierres=fopen('res.txt','a');
ftruncate($fichierres,0); 
...
fputs($fichierres, $t."\r\n");
...
fclose($fichierres);
echo'   <div style="text-align:center"><br>  <button id="download" width="100px" class="styled-button-8"><a href="res.txt" download="res.txt" style="color: #FFFFFF"><b>Download</b></a></button></div><br>';
wiwi
  • 29
  • 1
  • 3
  • possible duplicate of [Force to open "Save As..." popup open at text link click for pdf in HTML](http://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html) – Anwar Jul 23 '15 at 15:01

2 Answers2

0

Most browsers will auto-open any file that they can read - exactly how they should work. This includes .txt files, there's nothing that you can do to prevent this.

What you can do is provide the link as an anchor (<a href="/myfile.txt">Download</a>) and provide a message next to the link telling the user to "Right click / Save link as..." to download - this will allow them to save the file rather than download.

The exact option in the right click menu will differ between browsers but it's always something along the lines of "Save Link As...".

Kallum Tanton
  • 802
  • 7
  • 22
0

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

19.5.1 Content-Disposition

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file. This usage is derived from the definition of Content-Disposition in RFC 1806 [35].

content-disposition = "Content-Disposition" ":"
                      disposition-type *( ";" disposition-parm )
disposition-type = "attachment" | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = "filename" "=" quoted-string
disp-extension-token = token
disp-extension-parm = token "=" ( token | quoted-string )

An example is

Content-Disposition: attachment; filename="fname.ext"

In PHP, you would use header function to send this header. Note that this must be called before any data is sent.

header('Content-Disposition: attachment; filename="fname.ext"');
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98