Jump to content

Download files


Skype

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.