Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

file downloads

0 views
Skip to first unread message

Spadger M ClogToe

unread,
May 22, 2001, 2:08:02 PM5/22/01
to
Hi
i am a PHP newbie, and would like to know how to send a user a file from php

eg, if they goto

www.domain.com/file.php?file=requiredfile

they get given that assosciated file

i am ok with using an array or somehting else to tell PHP the right file to
send, just not how to make it send

thanks

jon


Matt D

unread,
May 22, 2001, 2:37:24 PM5/22/01
to
In message-id <tglaier...@corp.supernews.co.uk>
on Tue, 22 May 2001 19:08:02 +0100
'Spadger M ClogToe' issued the following statement:

there's two possible ways to do this;

one would be to 'fopen()' the target file and pipe the contents back
to the user, or probably the easiest would be to just redirect to the
target file:

[start-of 'file.php']
<?

header( "Location: $file" );

?>
[end-of 'file.php']

make sure there's no whitespace before the opening <? tag


HTH,
m.

--
#!/usr/local/bin/perl
print 'mailto:'.join('@',('emdee','cwcom.net'));

J Lillge

unread,
May 22, 2001, 2:46:44 PM5/22/01
to

>
> Hi i am a PHP newbie, and would like to know how to
> send a user a file from php
>
> eg, if they goto
>
> www.domain.com/file.php?file=requiredfile
>
> they get given that assosciated file
>

One method to do it is:

<?
readfile("$dir/$file");
?>

Where $file is the file that the user is requesting,
and $dir is the directory where it's located on your
server.

There are a few extra things that you might need to
do for the download. For example, if the $file is
not an HTML file, then you will have to set the
content-type. You must do this BEFORE your call
to readfile(). Example:

<?
header("Content-Type: image/gif");
readfile("$dir/$file");
?>

Another thing that you might want to do is to
force the browser to display a "Save File" box,
or to control the default name of the file on the
user's machine. That's done with this header:

<?
header("Content-Disposition: attachment; filename=$file");
readfile("$dir/$file");
?>

If you don't use this header, then the browser will
probably always save the file as the name of your
PHP script -- which is usually not what you want.


JL


Spadger M ClogToe

unread,
May 22, 2001, 6:03:01 PM5/22/01
to
thanks!

Spadger M ClogToe <spa...@poo.co.uk> wrote in message
news:tglaier...@corp.supernews.co.uk...

0 new messages