Download interface

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Download interface

Post by firkinfedup »

Hi there,

I was just wondering how the following is usually handled. I have created a download page in php which takes 1 id query as to the file that is requested. This is mainly so I can display download information and also increment my download counter.

The only problem being there is nothing to stop the destination URL from being used to starte the download anyway. So what is normally done under this circumstance? I need to monitor downloads of all my files via 1 page, so would it be a matter of copying the desired file to a temporary place on the server and then letting the client download that one? Then I run into issues as to when to delete the temporary file.

Anyway, just wondered, thanks loads in advance for your input!

Nick.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Check the referer to the page and this way you can ensure that the user came from your_page.php and not a foreign host. Then use header() to set the content type to whatever file type they are downloading and then echo the file data to the page. This will then make the user download the file - but will only work if they were sent to the download via your site.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the problem with http_referrer is that it is an optional header... if you want to make sure they are coming from somewhere else in the same domain, you could start a session on the previous pages.. and then test in download script that the session is active (meaning: they are coming from a page on your site)
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

aaaah

Post by firkinfedup »

Hi again,

That's cool, I didn't realise I could pass the file data manually. Would this mean that any resume support would be lost? I'm presuming I literally open the file into a byte array and send byte for byte the file.

Nick.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you mean something like this?

[php
if (isset($_GET['path']))
{
header('Content-length: ' . filesize($_GET['path']));
header('Content-type: application/octetstream');
header('Content-Disposition: attachment; filename=' . $_GET['path']);
readfile($_GET['path']);
}


probably with fopen, fseek, fread you can implement resume etc too..
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Post by firkinfedup »

Hi there.

That's excellent, pretty much what I had just gone away written. I think I'll set the mime type specifically, I'm presuming this will help with downloading the file straight into an application or ActiveX control?

Nick.
Post Reply