For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > June 2005 > how to download a file??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to download a file??
thamood@gmail.com

2005-06-11, 3:55 pm

Hello guys i'm new in this group but can you help me...i want to create
a "download files" system - something like "download.com" - but like
"download.com" i dont want the direct link to the file to show i want
it to be something like :

"http://www.myhomepage.com/getfile.php?id=132"

the idea is to optain the id through the "$_GET" and then get the path
of the file from the DB ...but then what?the "header()" function wont
work unless its the 1st function to be called so i cant query the DB
for the file path then call it!! or is there any other function i can
use to send the file to the user ???? can any one help me plzzzzzz it's
drivin me nuts... :))) thnx in advance...

Geoff Berrow

2005-06-11, 3:55 pm

I noticed that Message-ID:
<1118503366.200246.157530@f14g2000cwb.googlegroups.com> from
thamood@gmail.com contained the following:

>
>"http://www.myhomepage.com/getfile.php?id=132"
>
>the idea is to optain the id through the "$_GET" and then get the path
>of the file from the DB ...but then what?the "header()" function wont
>work unless its the 1st function to be called so i cant query the DB
>for the file path then call it!!



Yes you can. Just make sure you don't out put any html before you call
it
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Janwillem Borleffs

2005-06-11, 3:55 pm

thamood@gmail.com wrote:
> the idea is to optain the id through the "$_GET" and then get the path
> of the file from the DB ...but then what?the "header()" function wont
> work unless its the 1st function to be called so i cant query the DB
> for the file path then call it!! or is there any other function i can
> use to send the file to the user ???? can any one help me plzzzzzz
> it's drivin me nuts... :))) thnx in advance...


You obviously have miss-interpreted the info on http://www.php.net/header.

You cannot call header after any output, but you can call as many functions
you like before calling header() as long as they do not produce any output.

So, the following will fail:

<?php
echo strtoupper("hello");
header("Location: http://www.google.com");
?>

And the following will succeed:

<?php
strtoupper("hello");
header("Location: http://www.google.com");
?>


JW




thamood@gmail.com

2005-06-11, 3:55 pm

Thanx a lot guys for the clarification =EF=81=8A I'll try it again and then
see what happens thanx a lot=E2=80=A6

thamood@gmail.com

2005-06-11, 3:55 pm

Ahhh...guys sorry to bother ya again.....but what prameters should i
use with the header so i can download a file...(i'm really a
newbie)...i thought of useing the "Location :" but i figured it only
for redirection to some other web site....

Peter Chant

2005-06-11, 3:55 pm

thamood@gmail.com wrote:

> Ahhh...guys sorry to bother ya again.....but what prameters should i
> use with the header so i can download a file...(i'm really a
> newbie)...i thought of useing the "Location :" but i figured it only
> for redirection to some other web site....


File name! Something to stop the file getting cached, assuming you don't
want that. The below code is what I use for RTF files when I generate them.
Note that there is a problem with Galeon and RTF, so there is a workround
included in the code. Note the comment above CSV is out of date!

Pete



//The output data is generated dynamically, we do not want an old copy stuck
in a cache.

//HTTP 1.1
header("Cache-control: no-store, no-cache, must-revalidate");

//HTTP 1.0
header("Pragma: no-cache");

//Output a csv file, text/plain for a simple plain text file.
header("Content-Type: application/rtf");

//Force browser to save it
//Does not work on Galeon so create a cludge
$user_agent = $_SERVER["HTTP_USER_AGENT"];
if ( $filename != null ) {
if ( !eregi("galeon",$user_agent) ) {
header('Content-Disposition: attachment; filename="'.$filename.'"');
}
}


--
http://www.petezilla.co.uk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com