hi guyz,
how do u make a file downloadable?
say like the link is files.php?file=pmtct.ppt
and have it downloaded.
thanks
Printable View
hi guyz,
how do u make a file downloadable?
say like the link is files.php?file=pmtct.ppt
and have it downloaded.
thanks
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.
Content-disposition: attachment
Etc.
its the headers that i need to know, because all what i've tried does not seems to work properly.
pls help
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();
thanks penagate...but i'm confused
is $filename = $file in your code?
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.Quote:
Originally Posted by penagate
$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.
You need to check the file name contains no path characters:
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.PHP Code:$filename = preg_replace(array("/[a-z0-9_\-\.]/i","/^\.+/"),array('',''),$_GET['file']);
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
Post your code.
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();
}
Read post #9. You need to output the file contents, not the file name, your code is wrong.
my code contains whats exactly in post #9
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?
sorry... i meant post #5Quote:
Originally Posted by visualAd
and pls can you help re-produce a working version of my code in post #13
thanks
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.