Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2013, 07:55 PM   PM User | #1
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 45
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Automatically Save Files

I have a site that pulls information from a Database and one of the links I want to have the user download a file once they click on the link. I can't seem to pass the file name to the download php file.

the link for the files is <a href='download_file.php?fname=document_name.pdf'>

in the download_file.php file I have the following code, but it is not picking up the variable:

$fname = $_GET['fname'];

header('Content-disposition: attachment; filename={$fname}');
header('Content-type: application/pdf');
readfile('{$fname}');
chellert is offline   Reply With Quote
Old 02-05-2013, 08:15 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Nether the readfile nor the content-disposition will parse correctly. Single quoted strings are literal string's in PHP and all content within them are treated as literal strings.
PHP Code:
if (isset($_GET['fname']))
{
    
header('Content-type: application/pdf');
    
header('Content-disposition: attachment; filename="' $_GET['fname'] . '"');
    
readfile($_GET['fname']);

Now, this is *extremely* insecure. Nothing stops the user from reading any file off of the machine that this user account has access to. So you'll want to replace any characters that are illegal within the filename, in particular, the / character. Then what you can do is you can resolve the path, take the dirname off of it, and compare it to where you want to serve from. This process can be kludged together:
PHP Code:
$sRequestedPath realpath($_GET['fname']);
if (
dirname($sRequestedPath) == '/path/to/allowed/location')
{
    
// now you can process.

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
chellert (02-05-2013)
Old 02-05-2013, 10:22 PM   PM User | #3
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 45
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Thank you so much for that it worked
chellert is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:23 PM.


Advertisement
Log in to turn off these ads.