0

i have a webpage with a url going out. I want to change the filename that the person will receive.

<a href="http://downloadlink.com/Grizzly.avi">test - click here to download</a>
<?php
header('Content-Disposition: attachment; filename="test.avi"');
?>

The problem is: I receive the web page and not the file.. When i access the page, a download starts directly.. it's should wait for me to click on the "click here to download". Also, the downloaded file, is only the actual source code of the page and not the file that i want to download..

Here is a screenshoot, better to explain than my words with my bad english :D

https://i.stack.imgur.com/Si9cP.png

Thanks.

Sev3n
  • 33
  • 1
  • 1
  • 6
  • 1
    php source? or the html that your php generated? Either way, your above code cannot work, unless you have output buffering enabled, as you'll get [headers already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) errors. – Marc B Sep 15 '15 at 14:33
  • I receive the html source. So, if i understand well, i should put the header on the top of the code to fix that? – Sev3n Sep 15 '15 at 14:34
  • no, because then you'd never see the download link. – Marc B Sep 15 '15 at 14:35
  • you need conditional code `if (download link clicked) { do download stuff} else { show click-to-download page }` – Marc B Sep 15 '15 at 14:37
  • I did something like this now, but the php's content-disposition is still detected before i click it http://i.imgur.com/8J1Tj88.png – Sev3n Sep 15 '15 at 14:48
  • I think @MarcB was referring to do conditional code *inside PHP*. – al'ein Sep 15 '15 at 14:54
  • JS is client-side, while PHP is server-side.. Once the client opens your page, everything surrounded by `` tags will be executed before anything else. – al'ein Sep 15 '15 at 14:55
  • How should i do it please? – Sev3n Sep 15 '15 at 14:58
  • php runs on the server, js runs on the client. they will essentially NEVER run at the same time. – Marc B Sep 15 '15 at 14:58
  • marc just said... use JS to call PHP code via AJAX, **not in the same page**, but as a controller of your intents. – al'ein Sep 15 '15 at 15:00
  • With a window.location ? – Sev3n Sep 15 '15 at 15:02

2 Answers2

0

It very simple.

You just need to add download attr to your tag like this:

<a href="http://downloadlink.com/Grizzly.avi" download>test - click here to download</a>

It will start downloading.

For reference

Rahul
  • 5,594
  • 7
  • 38
  • 92
0

The Content-Disposition needs to go in the header for the file you are offering the user to download (i.e. the avi file in your example).

It doesn't go in the header for the HTML document with the link to the file.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335