Skype Posted April 10, 2009 Share Posted April 10, 2009 Hi, I have a 'premium' section on my site where members of the Forum (PHPBB3) can go to a control panel and download things. The files they can download are in a folder protected by .htaccess and a friend of mine suggested that you can use a php script to download these files. I have a file called checker.php which checks to see if the user is logged in before the download request is processed. <?php define('IN_PHPBB', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup(); ?> <?php if ($user->data['user_id'] == ANONYMOUS) { echo '<a href="members.php">Please login!</a>'; } else { $dir="/members/"; if (isset($_REQUEST["file"])) { $file=$dir.$_REQUEST["file"]; header("Content-type: application/force-download"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile("$file"); } else { echo "No file selected"; } } ?> It works fine and dandy, however the file that is downloaded is always corrupt. For example, if I access www.domain.com/forums/checker.php?file=test.zip it'll download fine, but when I go to open the zip it tells me it's corrupted. It happens with all kinds of files. Any help would be greatly appreciated, as I know hardly any PHP. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 You might add this in: header("Content-Transfer-Encoding: Binary"); as per this example: http://www.higherpass.com/php/Tutorials/File-Download-Security/ Quote Link to comment Share on other sites More sharing options...
Skype Posted April 11, 2009 Author Share Posted April 11, 2009 You might add this in: header("Content-Transfer-Encoding: Binary"); as per this example: http://www.higherpass.com/php/Tutorials/File-Download-Security/ Funnily enough that's the script I used. It tells me that the file I'm downloading is an MP3 file all the time. I edited it to this: $dir=("/members/"); if (isset($_REQUEST["file"])) { $file=$dir.$_REQUEST["file"]; header("Content-Type: $filedatatype/download"); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile("$file"); But now it tells me the download is always corrupt. I think it's something to do with the directory. Very annoying. Thanks. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 hmmm weird. try this example from php.net: <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> Quote Link to comment Share on other sites More sharing options...
Skype Posted April 11, 2009 Author Share Posted April 11, 2009 hmmm weird. try this example from php.net: <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> Sorry I don't quite think I explained myself. I have a folder called "members" which is where all the downloads are kept. This script is part of what checks to see if they're logged in before continuing the download. I noticed you put monkey.gif in the $file, so wouldn't this only work for a file called monkey.gif? I need it to work with zip files. So say I had many links, each one goes through the script. Sorry if I'm being a pain in the ass. Quote Link to comment Share on other sites More sharing options...
sandeep529 Posted April 11, 2009 Share Posted April 11, 2009 hi, Why dont you try uploading a simple text file and then open the uploaded file to check what went wrong..??? Quote Link to comment Share on other sites More sharing options...
Skype Posted April 11, 2009 Author Share Posted April 11, 2009 hi, Why dont you try uploading a simple text file and then open the uploaded file to check what went wrong..??? I tried that with a file called dttheme.zip. I download the zip and went to open it and it told me it was corrupt. Tried several times, same result. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 you should be able to use any file in that example Quote Link to comment Share on other sites More sharing options...
Skype Posted April 11, 2009 Author Share Posted April 11, 2009 you should be able to use any file in that example What's the monkey.gif for? Do I need to change that to anything? Thanks. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 11, 2009 Share Posted April 11, 2009 monkey.gif is an example. change it to your file. i just tested it on my server and works fine. Quote Link to comment Share on other sites More sharing options...
Skype Posted April 11, 2009 Author Share Posted April 11, 2009 monkey.gif is an example. change it to your file. i just tested it on my server and works fine. Where did you store your monkey.gif file? In the same directory as checker.php? Sorry it doesn't appear to be working for me.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.