1 (edited by FSX 2008-01-03 16:15)

Topic: Force download script corrupts files.

I don't know why. But when I download a torrentfile and open it in utorrent it says: Unable to load "torrentname": Invalid torrent file! And the same with archives (ace,zip,rar).
I tried .htaccess for force download, but that doesn't work 100%.

This is the script I made:

<?php
/***********************************************************************

  Copyright (C) 2007-2008  Frank Smit (fsx.nr01@gmail.com)

  This file is part of Download Mod.

  Download Mod is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  Download Mod is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/

define('PUN_ROOT', '../');
require PUN_ROOT.'include/common.php';
define('PUN_QUIET_VISIT', 1);

// Protection
if (intval($pun_config['dl_open_closed']) == 0)
    die('The download section is closed.');

if ($pun_user['id'] == 1 && $pun_config['dl_guest_access'] == 0)
    die('Guests are not allowed to download. Please <a href="register.php">register</a> or if you already are registered you can <a href="login.php">login</a>.');

// Set vars
$info        = explode (",", $_GET['d']);
$filepath    = $pun_config['o_base_url'].'/'.$pun_config['dl_download_dir'].'/'.$info['0'].'/'.$info['1'];

// Include cache file
if (file_exists('../cache/cache_download_data'.$info['0'].'.php'))
    require('../cache/cache_download_data'.$info['0'].'.php');

// Search in the cache for requested file
$filenr = getfilenr($files,$info['1']);

/* echo $info['0'].'<br />';
echo $info['1'].'<br />';
echo $filepath.'<br />';
echo $filenr.'<br />';
echo $files[$filenr]['filename'].'<br />';
echo $files[$filenr]['filetype'].'<br />';
echo $files[$filenr]['filesize'].'<br />'; */

$exclude_files    = explode(',', $pun_config['dl_exclude_files']);
$exclude_ext    = explode(' ', $pun_config['dl_exclude_ext']);
    
// Checking stuff
if(!in_array($files[$filenr]['filetype'], $exclude_ext) || !in_array($files[$filenr]['filename'], $exclude_files)) {
    
    // Update the download count
    $files[$filenr]['downloads']++;
    
    // Save data
    require_once PUN_ROOT.'include/cache.php';
    generate_download_data_cache($info['0'],$files);

} else die('You are not aloud to download this file. <a href="../download.php?c='.$info['0'].'">Click here to go back.</a>');

    // Set mimetype
    switch($files[$filenr]['filetype']) {
        case "pdf": $ctype="application/pdf"; break;
        case "exe": $ctype="application/octet-stream"; break;
        case "zip": $ctype="application/zip"; break;
        case "doc": $ctype="application/msword"; break;
        case "xls": $ctype="application/vnd.ms-excel"; break;
        case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
        case "gif": $ctype="image/gif"; break;
        case "png": $ctype="image/png"; break;
        case "torrent": $ctype="application/x-bittorrent"; break;
        case "jpe": case "jpeg": case "jpg": $ctype="image/jpg"; break;
        default: $ctype="application/force-download";
    }
    
    
    // Download :O
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".$files[$filenr]['filename']."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filepath));
    @readfile($filepath);
    exit();
    
?>