download file

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
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

download file

Post by nincha »

any way to allow some one to download a file without letting them know the directory the file is located on the server?
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

You could use apache and direct the link to something like

downloads.php?id=1

And store the links in a database then using $_GET and a mysql query to retrieve the data.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

how would that initialize the download?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

if(is_file('/home/path/to/directory/'.$gallery."/".$file)){
    header("Cache-control: private");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$file);
    header("Content-length: ".filesize($gallery."/".$file)."\n");
    //send file contents
    $fp=fopen($gallery."/".$file, "r");
    fpassthru($fp);
}
I found this snippet of code on this board today -- patrikG posted it. I'm using it to download images. But I'm sure it could be easily adapted for txt files or what not.

Hope that helps
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

nincha wrote:how would that initialize the download?
Depends on what they're downloading.. o_O
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

thnx neophyte, im curious about possible file corruption through this method of file download.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I've always done the downloading like in the file (doing a little effort to send the right content type)

http://timvw.madoka.be/programming/php/download.txt

AFAIK there is no problem with file corruption. But, you could always generate an MD5 file so your users can verify it...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

....MD5 and/or SHA1 and/or SHA256 ;)


teehee... I couldn't resist. :lol:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Actually, using SHA256 wouldn't necessarily be a good idea unless you have a large set_time_limit and you're caching the hashes. When you start dealing with message digests of files in the megabytes range, a pure PHP solution is just too slow.

Actually, there's no reason why you shouldn't be caching the hashes. SHA256 will just take a lot longer.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

cant seem to get this method of downloading to work correctly under https
Post Reply