I am building a file manager with php. Every request is handled by a php script, i.e. also direct download paths will be pre-checked so I can prohibit certain downloads and also display other content.
My problem now is, any here comes stackoverflow into play, that I do not know any solution how I can prepare the download aka display the content first, then refresh the page once so I can send the file ... but without side effects.
Refreshing is not a problem of course, but no refreshing solution works so far. Refreshing by header() keeps my page from displaying any content, refreshing with or javascript works regarding the display of the content, however, downloads with e.g. wget do not work anymore.
The problem in other words is:
Due to the prior content load a valid status code will be sent, thus programs like wget or curl will not follow the refresh, so it will not download the file, only the html.
To get the actual file with curl or wget you need to send http headers, however, the 3XX redirection status codes will need a value for Location. If header('Location: ..') is sent the website will not be displayed before the refresh.
A solution is required that sending appropriate status codes is possible and the content will still be loaded prior the file distribution.
One partially solution has been found with status code 206, which does not require a location value and still works with wget. curl actually does not. so maybe a better solution is still available?
Thanks for your help!
What about the <meta> tag?
<meta http-equiv="refresh" content="5" URL="http://your-url.com/redirect/">
Write this statement after printing content and it will wait 5 seconds before redirecting
<meta http-equiv="refresh" content="5;url=http://your-url.com/redirect/" />
Related
I wanted to serve a download of my program on special download page. Something like: http://site.com/download, where there will be a standard HTML page and also a file download prompt. I was wondering how I'd go about implementing this.
The only way I can think of is having a hidden iframe in the page pointing to the file the user wants to download. I also know of the PHP function readfile() but I don't see how I can implement that on the page aswell as have a HTML output shown to the user.
Any help is appreciated, thanks.
You can use a META redirect, which since it points to a download will not leave the page you're on.
On your HTML page, try including something like this:
<META HTTP-EQUIV="REFRESH" CONTENT="1;URL=/download/sitefile.zip">
This will browse to the file after 1 second, which should prompt the visitor to download it without leaving the page.
You can have the site http://example.com/download this can be normal html site. With redirection after few seconds (can be done in js or meta).
It should redirect to PHP site with fpassthru() function in it. That way you can easily implement additional security in the PHP.
HINT: make sure to set proper HEADERS in the PHP file so browsers will start download the file instead of showing the content in browser screen.
I have seen many websites triggering a file save-as dialog on a page with existing HTML content. How do they do this?
I know about setting the right headers such as Content-disposition etc. but when I do that, the content of the page does not load, and immediately the file download is triggered...
Make a regular HTML page, then add a META refresh that redirects to a PHP script that sends the file with a Content-Disposition header.
For example:
<meta http-equiv="refresh" content="2;url=download.php">
I know a lot of them use a hidden iframe, the page that the iframe points towards is actually a file download.
Although there are better ways to do this, I'm sure.
I'm trying to explain as best as I can, sorry for my English.
I have a list of links, each linked to a php file with an id by parameters (ex. download.php?id=1 or ?id=2 and so on).
This file create a new instance of a class witch return the correct header of the files so it displays the save dialog box of the browser.
Now I need to check if the files is already downloaded in past (The first time you downloaded it I add a field on the mysql db).
This checks go ahead if you haven't download the files, else return false.
Here is the problem, when it returns false or something else the browser redirect me to the download.php file, so I get a blank page or what I'm echoing.
I need that if the file is already download it show me a js alert for advice ppl.
Hope you can understand what i mean.
Thanks for help
Technically you can without ajax, the download.php can output the following if the user has already downloaded the file:
<script>
alert('It was false - you already have the file!');
window.back();
</script>
Just depends how well it integrates with your site. Not tested but thats the general idea.
it's too late to show a js alert in that case, you'd need to do something like ajax to check whether the file has been downloaded, and then show the alert or start downloading the file then.
once your web browser has started loading a new url (eg download.php) then it is too late, you are already navigating away from the current page.
So you've got a page that looks like:
1. file1
2. file2
3. file3
and they've got links to the download script on each. If you want to prevent multiple downloads, you do it in two places. Here on the main file list page, and on the download.php page. When the user clicks on one of the files, you have an onclick handler remove the link from the clicked file. This can be done by refreshing this page (and simply not adding the download link when the page is generated), or using some DOM manipulation to remove the tag around the filename.
The download page will also do checks if the file's been sent previous and can handle that condition itself.
Doing so in both places will degrade nicely if for whatever reason the client doesn't have Javascript enabled.
You need to handle it in two places for the best behavior.
Determine if the user can download the file when you generate the download page, and don't create links for files that can't be downloaded.
To handle the case of someone having multiple windows open or otherwise downloading a file without reloading the downloads page, check if the user can download the file in download.php before sending any headers. If he can't, send a redirect back to the download page:
header("Location: downloads.php?error=repeat_download");
exit;
…and use the error parameter to include a message at the top of the file list explaining what happened.
I need a PHP script running on my server to frequently request a web page.
I thought of using the PHP header function combined with a meta refresh tag, but that won't work because the header will redirect to the URL immediately, and the meta refresh will never execute.
<?php
header('Location: http://www.example.com/');
?>
<html>
<META HTTP-EQUIV=Refresh CONTENT="60">
</html>
Does anyone have any suggestions for how to do this please?
If you want to do this using PHP alone, you'll need to change your solution a bit. Instead of sending an HTTP Location: header, redirecting the user away from the page, you'll want to load the remote contents into a variable yourself using file_get_contents. Then you can rewrite all the URLs and inject your refresh tag into the HTML, and output that.
A far easier solution would be to make an iframe and set that up to be refreshed using JavaScript. If you like I could provide a code sample, just ask.
If I understand your question correctly, you want to automatically refresh a page each time after a certain timeout.
The header generated by the php code is intended to directly redirect to some URL, so take it out because you will not be able to set a time out that way.
It is the client (that is, the webbrowser that views the page) that must reload the page after some timeout, not your webserver. Server side scripting (such as PHP) will not help you therefore.
The client can be instructed to reload the page using that <META ...> tag, or with some javascript:
<script type='text/javascript'>
setTimeout(function () {
window.location.reload(true);
}, 60000); // reload after 60 seconds
</script>
I have a php-apache website on which I am trying to track download conversions using Google Analytics. I want my users to initiate the download and be redirected to a "thank you" page in one click. The way I'm envisioning this is:
The user clicks one of several download buttons which sends them to a generic thankyou.php page, and passes a variable telling that page which file to give them. Thankyou.php contains a header which uses that variable to start a download dialogue.
If there are better ways to do this, I am open to anything. To my bewilderment, I haven't found a good way to do this after several hours of poking around here and on Google.
Send the file after the ThankYou page has been loaded.
You could either use an iframe for this (if this is allowed in your (X)HTML variant), e.g.
<iframe src="download.php?id=123" style="display:none;" />
or a meta refresh, e.g.
<META http-equiv="refresh" content="1;URL=download.php?id=123">
or use JavaScript or whatever is able to call a URL. You could then write something like your download should start automatically in a second. If not please click this link, where the link is href'ed to download.php?id=123 as well.
In download.php you'd just send the regular headers for sending a file and pass it to the client. Check this question's answers to see how.
Also see this related question.