Click to See Complete Forum and Search --> : Downloading a file


aaronuni
06-21-2007, 02:12 PM
I also posted this in the JavaScript section, hoping to find a solution there...
I'm working on a php program that allows instructors to see a list of classes that they are currently teaching as links which take them to a page of students that are in that class. If the student has submitted a paper (in .doc format), a link to the file is displayed beside the student's name. The uploading of the file is taken care of by a separate program. Getting the file to download is no problem. My problem is that I need to track in the database if the file has been downloaded or not. Instead of making just a straight link to the file, I'm making the link go to a php function where after doing database work, I build a string containing the JavaScript window.open(path_to_file) which I pass to another page along with an updated list of students. On the new page, I get both the list of students and a file download dialog box in Firefox, but in IE, I only get the list of students. Pop-ups aren't getting blocked, as I initially thought, because it works when I have the JavaScript in the original page with the list of students. Any thoughts? I'd like to hear any suggestions on how I can handle this better. I can show code if needed.

Kyleva2204
06-21-2007, 03:28 PM
okay.. uhm I think you are doing a bit unnecessary work. When you reach the point "Im making the link go to a php function where after doing database work". Thats all you should have to do. When you finnish with the database. Also in the PHP File place this:


header("Content-Type: " . mime_content_type($url_to_file));
echo file_get_contents($url_to_file);


If you have those two things in the PHP file and nothing else is outputted (Print, Echo, etc), then the file will download, or be displayed as it normally would.

Hope this helps :).

aaronuni
06-26-2007, 04:33 AM
Thanks for the reply. I think I'm on the right track now by using the MIME type and the header function.


header('application/x-force-download');
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);

The above code works in multiple browsers, but I'm still not able to get a 2nd page to display. I need to present the user with a Web page where they can confirm that they have downloaded the file. I believe that it is possible to have a multipart/mixed MIME Content type, and then send both types as a header. One (application/msword) would present a download dialog box, and the other (text/html) would have the browser render an html page at the same time. Does anyone know how to do this?

bluestars
06-27-2007, 10:24 PM
Hmm... so you want the user to click the download link, then click ANOTHER box to confirm that they actually downloaded the file, at which point you'll remove the file from the server?

You could add an onClick event to your link that displays an alert box (in JS), which has a yes/no choice. Both lead to a php page with a param tacked onto the URL. So, yes goes to /downloadcheck.php?a=yes and no to /downloadcheck.php?a=no. Then you can do your DB work from those pages.

Ahh... on second thought, I just tried that, and it didn't work... Maybe open the page in a new window? Hold up...

Alright, so that works. I wonder if it's possible to open a window in JS, then affect the PARENT window. This is definitely a question for the Javascript guys, not us.