|
-
Jan 17th, 2007, 08:10 AM
#1
Thread Starter
Fanatic Member
File Download
hi guyz,
how do u make a file downloadable?
say like the link is files.php?file=pmtct.ppt
and have it downloaded.
thanks
-
Jan 17th, 2007, 08:23 AM
#2
Re: File Download
Either you redirect to the actual location of the file, or you generate the appropriate headers and just pipe the entire content of the file through to the client.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 17th, 2007, 08:29 AM
#3
Re: File Download
Content-disposition: attachment
Etc.
-
Jan 17th, 2007, 09:02 AM
#4
Thread Starter
Fanatic Member
Re: File Download
its the headers that i need to know, because all what i've tried does not seems to work properly.
pls help
-
Jan 17th, 2007, 09:05 AM
#5
Re: File Download
PHP Code:
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: text/plain'); // Replace as appropriate
header('Content-disposition: attachment; filename="'.str_replace('"', '\'', $filename));
header('Content-transfer-encoding: binary');
header('Content-Length: '.strlen($file));
echo $file;
exit();
-
Jan 17th, 2007, 09:15 AM
#6
Thread Starter
Fanatic Member
Re: File Download
thanks penagate...but i'm confused
is $filename = $file in your code?
-
Jan 17th, 2007, 09:16 AM
#7
Re: File Download
No; they're what they say they are.
-
Jan 17th, 2007, 09:27 AM
#8
Thread Starter
Fanatic Member
Re: File Download
 Originally Posted by penagate
No; they're what they say they are.
if so how do you came about $filename...though i know $file is from the $_GET querystring.
-
Jan 17th, 2007, 09:30 AM
#9
Re: File Download
$filename is the name of the file. $file is the file contents.
As an aside, I hope you are not simply opening the file name that is passed in the query string. Otherwise, that's a major, major, security risk.
-
Jan 18th, 2007, 04:36 AM
#10
Re: File Download
You need to check the file name contains no path characters:
PHP Code:
$filename = preg_replace(array("/[a-z0-9_\-\.]/i","/^\.+/"),array('',''),$_GET['file']);
That will use a regular expression to replace any non file name characters and any dots at the beginning of the file name with a null string.
-
Jan 18th, 2007, 06:36 PM
#11
Thread Starter
Fanatic Member
Re: File Download
i don't know why its not working.... the file in question is about 2.5MB(in size), and when i try to download it gives me 26 bytes(we all know that shouldn't be).
pls help
-
Jan 18th, 2007, 07:00 PM
#12
-
Jan 18th, 2007, 07:42 PM
#13
Thread Starter
Fanatic Member
Re: File Download
PHP Code:
$file = $_GET['file'];
$filename = preg_replace(array("/[a-z0-9_\-\.]/i","/^\.+/"),array('',''),$file);
if(file_exists($file)){
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/octet-stream'); // Replace as appropriate
header('Content-disposition: attachment; filename="'.str_replace('"', '\'', $filename));
header('Content-transfer-encoding: binary');
header('Content-Length: '.strlen($file));
echo($file);
exit();
}
Last edited by modpluz; Jan 18th, 2007 at 07:47 PM.
-
Jan 18th, 2007, 08:04 PM
#14
Re: File Download
Read post #9. You need to output the file contents, not the file name, your code is wrong.
-
Jan 18th, 2007, 08:08 PM
#15
Thread Starter
Fanatic Member
Re: File Download
my code contains whats exactly in post #9
-
Jan 18th, 2007, 08:17 PM
#16
Re: File Download
There is no code in post #9. Read the post. Like we have already said. You need to get the contents of the file and use echo() to output it.
You code simply outputs a file name, how can you expect that to work?
-
Jan 19th, 2007, 09:39 AM
#17
Thread Starter
Fanatic Member
Re: File Download
 Originally Posted by visualAd
There is no code in post #9
sorry... i meant post #5
and pls can you help re-produce a working version of my code in post #13
thanks
-
Jan 19th, 2007, 10:07 AM
#18
Re: File Download
Post number 5 contains the code you need and its working code. $file is the contents of the file and $filename is the name of the file. All you need to do is get the file contents.
If you can't work out how to do that you might be better off paying someone to do it for you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|