Need help in downloading file with header() function - php

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.

Related

Can I know on PHP (without javascript) if a user has right-clicked oppening a link?

I was coding from some ideas collected over the web a "solution" to redirect all pdf, for example, downloads on a server to a reCAPTCHA form and, after passed, download the requested file. It is cms agnostic and works but I have one situation that downloads an incorrect file.
The code is at: https://github.com/Siot/docafi (request.php file)
I explain the process:
All .pdf requests where redirected to request.php through .htaccess:
RewriteRule ^(.+.pdf)$ request.php?file=$1 [NC,L]
If it's the first time ($_SESSION var) request.php will show you a reCAPTCHA form.
If reCAPTCHA passed -> download starts.
If error -> error message.
If reCAPTCHA was passed previously on the same session, download starts automatically.
The special case is when is the first time in the session but starts download using right-click over the link.
It downloads an incorrect file content because I can't show the reCAPTCHA form. Can I know if a user had right-clicked over the link without javascript? I want a transparent solution over the different cms/html files on the server.
Can I solve this on another way?
No, selecting 'Save as...' in the popup menu works exactly as it should - it saves whatever is fetched by the link to local disk instead of trying to show it in the browser. It's thus completely correct that it's downloading the HTML file with the reCAPTCHA instead, and there's no way to detect it, not even with Javascript - to the server it's all the same, since the browser issues the same request, it just processes the result differently. You could set a flag on onContextMenu event, but it'd just have the wrong setting anyway if you then opened the link by leftclicking it or pasting the address in the address bar by hand.
It should be possible with javascript however PHP i'm fairly confident there will be no way this can be done considering it is server side.
Look through some previous stack overflow questions like:
how to get right click event javascript

Prompting a file download on my web page

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.

Grab the page which is redirecting

I am trying to grab a file .pdf from a server. There is a hyperlink at the page, by clicking that link it goes to a page, it checks for some privileges, then it redirects to another page which shows the content of the .pdf within an Iframe.
lets say beginning url is http://site.com/docs/1.pdf
on click it goes to another page, then another one and it comes whth the last page
http://site.com/viewer/pdfs/1.pdf
the last page shows the pdf content within an Iframe.
I realized that the software IDM (Internet download manager) can follow the redirections and download the file by clicking the first link.
I was wondering if there is an algorithm or library or class or hint that I can figure out how to do that in PHP scripting.
by the way, once I wrote a code to read the header of the page and I could redirect to the second page, but I want to know if there is a general algorithm for this or not.
If you are doing the HTTP stuff manually, check for 30x statuscodes and the Location header.
However, you could simply use CURL and set CURLOPT_FOLLOWLOCATION.
Yes, just like ThiefMaster said, you could look for the Location header.
Have a look here, maybe this can be a help to you:
http://codesnippets.joyent.com/posts/show/1214 This function retrieves file size of a remote file, why don't you try to change it slightly so that it gets the final URL?

Hide PDF URL using JS

I have a php page . I need to make it hard for user to get direct download link.For this i need a js function which start downloading pdf after 10sec automatically after page load. I dont want to provide a download link at all. Also I cant use onpageload . PDF must download.
Since most of the browsers will tell you where the downloaded file came from, i think you may want to mask the file itself behind a 'temporary' link with mod_rewrite or other custom parameters. You don't need to use JavaScript for this.
After then you can simply push the file with PHP similarly like this solution.
There is absolutely no way to hide a URL from an end user - All they need to do is use Fiddler 2, Firebug or similar tools to view the requested URL
Have your download page redirect to the PHP file that will download it. If it can download it, it will redirect the user back to the previous page, because the download doesn't have the right content type, although it could be just a plain .html file. You don't need Javascript to do this:
<meta http-equiv="refresh" content="10;url=http://mysite/d.php?file=resume">
I recommend the Smart File Download, from zubrag.com, if you don't already have a PHP file specifically for downloading.

Force PHP download script pops up a blank page, is there a way to close it?

The PHP file is ran with headers to force a download (from flash/as3 project), but it also pops up a blank page. Is there a way to close that after?
I don't think users want blank page popups springing up.
What I thought would work, but apparently not:
echo ("<script>window.close();</script>")
You can't echo something since that's part of the output of the script producing the file they're downloading. You wrote that JavaScript into the file. There is no separate webpage where you can put code like that.
You take care of it on the code that's starting the download (tricky stuff like javascript writing a 1x1 pixel iframe pointing to the download URL) or accept that you don't control browser behavior like this.

Categories