I have found numerous examples on forcing he download of a file with PHP, but none of them show how to do this from a new page (e.g. a thank you for downloading page, with a button to start download if it didn't start already). When ever I use the header attachment method it just downloads the file whilst leaving the browser on the page I was on when I click the link.
I want to be able to show a new page and automatically start the download. How do I do this with PHP?
It can be done by meta refresh on the "Thank you page" that you want where 5 is in seconds, change it to be a longer delay if you want.
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=/download.php">
Then your download script where you're pointing at URL=/download.php should do the force download to what ever file it needs to serve.
Related
I'm trying to make a product overview page where a user can download an invoice. However this invoice needs to be generated to the specific product.
At the moment the way i do this is to navigate to invoice.jsp which sends the ID to the server which in turn creates the invoice.pdf, after this is created the user can push a button to download the just created file.
However I would like to skip the last step and trigger the download as soon as the page is loaded and redirect back to the orderoveriew.jsp
Make an iframe on orderoveriew.jsp with the pdf code. Just remember to set
header('Content-Type: application/force-download');
and it will download automatically, other methods might not work with pop-up blockers.
I've got a "download.php" script which serves up files for download and the user can choose to download a ZIP file which is custom to them.. so needs to be generated on the fly.
The problem I've got, is if the user clicks the ZIP file 5 times in quick succession, the ZIP is generated 5 times on the server.
I need a method to only generate this on the first click, and not keep generating over and over again.
I do not want to "disable" the link permanently, because downloading the file again a few minutes later is fine if the user wants to, I just want to try and stop a malicious user hammering the download script to generate ZIPs constantly using server processing power.
Any ideas? I've been trying for hours and can't seem to get a solution which works :/
Thanks
Is it an option to include a captcha code or similar to activate the download? That would stop the fast clicking.
Add jQuery to the front page to disable the button after the first click. Something like:
$("#button").click(fuction(e) {
$(this).attr("disabled", "");
});
Also, you could cache the zip files for a certain amount of time so their only processed once for a thirty minute block, or some time frame.
I have a PHP script that downloads a PDF file from the server and prompts you to either open it, or save it. The script accepts a one time token, which is used in place of a file name, to hide the file name.
If you go to the actual php page, http://example.com/files/download/token the script works fine and it downloads the PDF.
I could just send people to that page with a standard link tag, but once the file downloads I need to update content on that page which is returned through that download script.
Is there any way to have ajax call open up a new window where the file will download and then return the data that I need to update the current page?
There is more to the download script, but the main piece is the actual downloading part:
header("Content-type: application/pdf");
$this->load->helper('file');
readfile("static/temp_statements/".$local_file_name);
unlink("static/temp_statements/".$local_file_name);
One trick that I have used in the past that might be useful to you is
Supply a query param in your ajax call to download the PDF. This will be a unique name.
The server process that streams the PDF for download sets a cookie with this unique name.
You poll in your page waiting for this cookie to appear.
When the cookie appears you can assume the file has downloaded, and you can do your contingent action.
And you do not need to open a window to make this happen. You could just append an invisible iframe like this:
$(some selector).append($("<iframe width='1' height='1' frameborder='0' src='" + url + "'></iframe>"));
In the interests of honesty and transparency, I originally found this idea from this SO answer and it worked for me: Detect when browser receives file download
I have a search page.
On clicking search it will show a jquery modal with processing image and redirect to results page.
I have download functionality also in the search page. If I select the download option,
the result may vary to 1 kb to 25 MB, and I can't put a timer to close the modal window.
Is there any way to find the download is prompted?
Or download is completed? So that I can close modal at that time!!
Just make a popup that disappears when the user click anywhere else, like on github.
It would probably be too much job doing what you requests that it is not worth it. Better just showing a dialog that the user can dismiss whenever s/he wants to.
Well, yes it would be possible to have a script that runs while the file is being served (PHP normally times out within 30sec) and have periodically AJAX-requests to see if download is completed (or aborted).
I have a web form that users complete online. When they press submit it will start a file download for them.
At the moment, I process the form submission and generate a suitable file for the user and fire it off with suitable headers. eg...
header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");
However, since this starts a download right away, the original form is left on screen by the browser.
I would really like to go to some "Thank you" screen once the download completes (or before the download starts). I know it is possible, because almost every download site you visit does this (normally to pump you full of adverts before the download starts).
So, How do I show a "Thank You" screen that starts the download after a second?
How would any solution proposed effect the behaviour of the back button, as I don't want the file downloading again without the form being refilled?
I am using PHP on the server and can rely on Javascript (and jQuery) being available on the client.
Thank you.
You could send the form to the Thank you document and put there a META refresh to the file download:
<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the file</p>
Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:
$(document).ready(function(){
window.setTimeout(function(){
window.location = 'http://yourdownloadhost.com/file.zip';
}, 1500);
});
or use a meta redirect.
You can insert a hidden iframe into your page and submit your form to this iframe.