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
https://forums.phpfreaks.com/topic/153545-download-files/
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
https://forums.phpfreaks.com/topic/153545-download-files/#findComment-806954
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
https://forums.phpfreaks.com/topic/153545-download-files/#findComment-806958
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
https://forums.phpfreaks.com/topic/153545-download-files/#findComment-806961
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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