/    Sign up×
Community /Pin to ProfileBookmark

Downloading a file

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Kyleva2204Jun 21.2007 — 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:

<i>
</i>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 ?.
Copy linkTweet thisAlerts:
@aaronuniauthorJun 26.2007 — 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?
Copy linkTweet thisAlerts:
@bluestarsJun 28.2007 — 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.
×

Success!

Help @aaronuni spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...