1

Topic: "Cannot modify header information" problem

I got this error showing up when I enter my download page:

Warning: Cannot modify header information - headers already sent by (output started at /home/fsx61924/public_html/mithrill/download.php:2) in /home/fsx61924/public_html/mithrill/header.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/fsx61924/public_html/mithrill/download.php:2) in /home/fsx61924/public_html/mithrill/header.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/fsx61924/public_html/mithrill/download.php:2) in /home/fsx61924/public_html/mithrill/header.php on line 33

Warning: Cannot modify header information - headers already sent by (output started at /home/fsx61924/public_html/mithrill/download.php:2) in /home/fsx61924/public_html/mithrill/header.php on line 34

I tried some thing but I don't really know how to solve it. The download isn't working either, when I click download my browser shows all kinds of weirds characters.

This is the source of my download page.:

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

// Load the index.php language file
require PUN_ROOT.'lang/'.$pun_user['language'].'/index.php';

// Config
$dldir            = $pun_config['dl_download_dir'];
$page_limit        = $pun_config['dl_files_per_page'];

// Url stuff
$_GET            = array_map('addslashes', $_GET);
$_GET            = array_map('trim', $_GET);
$category        = $_GET['c'];
$by                = empty($_GET['by']) ? 'date' : $_GET['by'];
$sort            = $_GET['sort'] == 'DESC' ? 'ASC' : 'DESC';
$sortp            = $_GET['sort'] == 'ASC' || $_GET['sort'] == 'DESC' ? $_GET['sort'] : '';
$getp            = empty($_GET['p']) ? 0 : $_GET['p'];

// Download the file
if (!empty($_GET['d'])) {
    // Set vars
    $info        = explode (",", $_GET['d']);
    $info['3']     = findexts($info['1']);
    $filepath    = PUN_ROOT.$dldir.'/'.$info['0'].'/'.$info['1'];
    
    if (!file_exists($filepath))
        message('File does not exist.');
    
    // Checking stuff
    if (filterlist($filename,$info['0'],$info['3']['0'],$info['3']['1'],true)) {
        
        // Update bandwidth of user
        $db->query('UPDATE '.$db->prefix.'users SET dl_total_bandwidth=dl_total_bandwidth+'.$info['2'].' WHERE id='.$pun_user['id']) or error('Unable to update total bandwidth of user', __FILE__, __LINE__, $db->error());
    
        // Write to log
        $logfile = $dldir.'/.log';
        $fopen = fopen($logfile, 'a') or die('can\'t open file');
        fwrite($fopen, $pun_user['username'].' - '.date("Y-m-d/H:i:s", time()).' - '.$info['0'].'/'.$info['1']."\n");
        fclose($fopen);
        
        // Check if file can be opened, if so, download it
        $fp = @fopen($filepath, "rb");
        if(!$fp) {
            die('Bad request');
        } else {
            $filepath = rawurlencode($filepath);    // fix filename (spaces may still mess things up, perhaps add a specific MSIE thing later, not sure though)

            header('Content-Disposition: attachment; filename="'.$info['1'].'"');
            header('Content-type: application/octet-stream');
            header('Pragma: no-cache');
            header('Expires: 0'); 
            header('Connection: close');
            header('Content-Length: '.$info['2']);
            fpassthru($fp);
        }
    
    } else message('You are not aloud to download this file. <a href="download.php?c='.$info['0'].'">Click here to go back.</a>');
    
    exit;
}

// Stuff for the search function
if (isset($_POST['form_sent']) && !empty($_POST['searchword']) && !empty($_POST['category'])) {
    session_name('dlmod_search');
    session_start();
    
    
    if (!isset($_SESSION['initiated'])) {
        session_regenerate_id();
        $_SESSION['initiated'] = true;
    } 
}

// Insert search stuff in a session
if (isset($_SESSION['initiated']) && isset($_POST['form_sent']) && !empty($_POST['searchword']) && !empty($_POST['category'])) {
    
    $searchvar = array_map('trim', $_POST);
    
    $_SESSION['search'] = array(
        'searchword'     => ereg_replace('[^a-zA-Z0-9 _ -]', '', $searchvar['searchword']),
        'searchby'         => intval($searchvar['searchby']),
        'category'         => addslashes($searchvar['category'])
    );
    
    if ($category != $_SESSION['search']['category'] && is_dir($dldir.'/'.$_SESSION['search']['category'])) {
        header('location: download.php?c='.$_SESSION['search']['category']);
        exit;
    }
}

// Clean out sesson if user goes to an other category
if ($category != $_SESSION['search']['category'] || isset($_GET['clean']))
    $_SESSION['search'] = NULL;

// Read dirs
if (!empty($category)) {
    
    // Read dir for filelist
    $dir    = $dldir.'/'.$category;
    $dh        = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        
        $filetype = findexts($filename);
        
        if (filterlist($filename,$category,$filetype['0'],$filetype['1'],true)) continue;

        $filestats    = stat($dldir.'/'.$category.'/'.$filename);
        $files[]    = array('filename' => $filename , 'filenamenoext' => $filetype['0'] , 'filesize' => $filestats['7'], 'filetype' => $filetype['1'], 'filedate' => $filestats['mtime']);
    }
    
    $filecount = count($files) - 1;
    $cfilecount = $filecount + 1;
    
    if ($cfilecount != 0) {
        if        ($by == 'name') foreach($files as $res) $sortAux[] = $res['filename'];
        elseif    ($by == 'size') foreach($files as $res) $sortAux[] = $res['filesize'];
        elseif    ($by == 'type') foreach($files as $res) $sortAux[] = $res['filetype'];
        elseif    ($by == 'date') foreach($files as $res) $sortAux[] = $res['filedate'];
    
            
        if        ($sort == 'ASC')    array_multisort($sortAux, SORT_ASC, $files);
        elseif    ($sort == 'DESC')    array_multisort($sortAux, SORT_DESC, $files);
    }
    
    
} else {
    
    // Read dir for download index list
    $cdir    = $dldir;
    $cdh    = opendir($cdir);
    while (false !== ($cname = readdir($cdh))) {
        
        if (filterlist($cname)) continue;
        
        $cstats = stat($dldir.'/'.$cname);
        $cdirs[] = array('cname' => $cname ,'cdate' => $cstats['mtime']);
    }
    
    $ccount = count($cdirs) - 1;
    
    if        ($by == 'name') foreach($cdirs as $res) $sortAux[] = $res['cname'];
    elseif    ($by == 'date') foreach($cdirs as $res) $sortAux[] = $res['cdate'];
        
    if        ($sort == 'ASC')    array_multisort($sortAux, SORT_ASC, $cdirs);
    elseif    ($sort == 'DESC')    array_multisort($sortAux, SORT_DESC, $cdirs);

}

// Read dir for category menu
$menudir    = $dldir;
$menudh     = opendir($menudir);
while (false !== ($dirname = readdir($menudh))) {
    if (filterlist($dirname)) continue;
    
    $dirsmenu[] = $dirname;
}
@sort($dirsmenu);
    
// Set title
$cattitle = !empty($category) ? '/ '.str_replace('_',' ',$category) : '';
$page_title = pun_htmlspecialchars($pun_config['o_board_title'])." / Downloads $cattitle";
require PUN_ROOT.'header.php';

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

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

?>

<div class="block2col">

    <div class="blockmenu">
        <h2><span>Download menu</span></h2>
        <div class="box" style="">
            <div class="inbox">
                <ul>
                    <li><a href="download.php">Download Index</a></li>
                    <li><hr style="height: 0;border: 0;border-top: 1px solid #BFBFBF;" /></li>                    
                    <li><select onchange="window.location='download.php?c='+this.value" style="width: 100%">
                        <option value="">Select category</option>
                        <?php foreach ($dirsmenu as $menuitem) { $selected = $menuitem == $category ? ' selected="selected"' : ''; echo '<option value="'.$menuitem.'"'.$selected.'>'.str_replace('_',' ',$menuitem).'</option>'."\n"; } ?>
                    </select></li>
                    
                </ul>
            </div>
        </div>
    </div>
    
    <?php if (!empty($category)) { 
        
        // Get information file
        if(file_exists($dldir.'/'.$category.'/.info')) {
            
            require PUN_ROOT.'include/parser.php'; ?>
            
            <div class="block">    
                <h2><span>Information about <?=str_replace('_',' ',$category)?></span></h2>
                <div class="box">
                    <div class="inbox" style="padding:5px">
                        <?php 
                        $dext = file_exists($dldir.'/'.$category.'/_descimg.gif') ? 'gif' : file_exists($dldir.'/'.$category.'/_descimg.jpg') ? 'jpg' : 'png';
                        if(file_exists($dldir.'/'.$category.'/_descimg.'.$dext)) echo '<img style="float:right;margin: 5px 3px 5px 5px" src="'.$dldir.'/'.$category.'/_descimg.'.$dext.'" alt="" />'; echo parse_message(file_get_contents($dldir.'/'.$category.'/.info'), 0); ?>
                        <div class="clearer" style="clear:right !important"></div>
                    </div>
                </div>
            </div>
            <br />
            
        <?php } 
        
        if ($cfilecount == 0) { ?>
            
            <div class="blockform">
                <h2><span>File search</span></h2>
                <div class="box">
                <form id="filelist" method="post" action="download.php?c=<?=$category?>">
            
                    <input type="hidden" name="form_sent" value="1" />
                
                    <div class="inform">
                        <fieldset>
                            <legend>Find files</legend>
                            <div class="infldset">
                                <label class="conl">Filename<br /><input type="text" name="searchword" value="<?php if (isset($_SESSION['search'])) echo $_SESSION['search']['searchword']; ?>" size="25" maxlength="255" /><br /></label>
                                
                                <label class="conl">Search by
                                <br /><select name="searchby">
                                        <option value="1">Filename</option>
                                        <option value="2">Extension</option>
                                </select>
                                <br /></label>
                                
                                <label class="conl">Category
                                <br /><select name="category">
                                        <?php foreach ($dirsmenu as $menuitem) { $selected = $menuitem == $category ? ' selected="selected"' : ''; echo '<option value="'.$menuitem.'"'.$selected.'>'.str_replace('_',' ',$menuitem).'</option>'."\n"; } ?>
                                </select>
                                <br /></label>
                                <p class="clearb">Enter a filename to search or search by extension/filetype.</p>
                            </div>
                        </fieldset>
                    </div>
                    <p><input type="submit" name="search" value="Submit" accesskey="s" /> <?php if (isset($_SESSION['search'])) echo '<a href="download.php?c='.$category.'&clean">View normal filelist.</a>'; ?></p>
                </form>
            
                </div>
            </div>
            <br />
        
            <div class="block box" style="text-align:center;padding:10px">
            <?php if (isset($_SESSION['search'])) { ?>
                <span>No files found.</span>
            <?php } else { ?>
                <span>There are no files in this category.</span>
            <?php } ?>
            </div>
                    
            <?php
                    
        } else {
            
            ?>
            
            <div class="blockform">
                <h2><span>File search</span></h2>
                <div class="box">
                <form id="filelist" method="post" action="download.php?c=<?=$category?>">
            
                    <input type="hidden" name="form_sent" value="1" />
                
                    <div class="inform">
                        <fieldset>
                            <legend>Find files</legend>
                            <div class="infldset">
                                <label class="conl">Filename<br /><input type="text" name="searchword" value="<?php if (isset($_SESSION['search'])) echo $_SESSION['search']['searchword']; ?>" size="25" maxlength="255" /><br /></label>
                                
                                <label class="conl">Search by
                                <br /><select name="searchby">
                                        <option value="1">Filename</option>
                                        <option value="2">Extension</option>
                                </select>
                                <br /></label>
                                
                                <label class="conl">Category
                                <br /><select name="category">
                                        <?php foreach ($dirsmenu as $menuitem) { $selected = $menuitem == $category ? ' selected="selected"' : ''; echo '<option value="'.$menuitem.'"'.$selected.'>'.str_replace('_',' ',$menuitem).'</option>'."\n"; } ?>
                                </select>
                                <br /></label>
                                <p class="clearb">Enter a filename to search or search by extension/filetype.</p>
                            </div>
                        </fieldset>
                    </div>
                    <p><input type="submit" name="search" value="Submit" accesskey="s" /> <?php if (isset($_SESSION['search'])) echo '<a href="download.php?c='.$category.'&clean">View normal filelist.</a>'; ?></p>
                </form>
            
                </div>
            </div>
            <br />
            
            <div class="block">
                <h2><span><?=str_replace('_',' ',$category)?> - <?=$cfilecount?> files</span></h2>
                <div class="box" style="padding:0">
                    <div class="inbox" style="padding:0">
                
                        <table cellspacing="0">
                        <thead>
                            <tr>
                                <th class="tcl" scope="col" style="width:46%"><a href="download.php?c=<?=$category?>&p=<?=$getp?>&by=name&sort=<?=$sort?>">Filename</a></th>
                                <th class="tc2" scope="col" style="width:15%"><a href="download.php?c=<?=$category?>&p=<?=$getp?>&by=size&sort=<?=$sort?>">Size</a></th>
                                <th class="tc3" scope="col" style="width:15%"><a href="download.php?c=<?=$category?>&p=<?=$getp?>&by=type&sort=<?=$sort?>">Type</a></th>
                                <th class="tcr" scope="col" style="width:24%"><a href="download.php?c=<?=$category?>&p=<?=$getp?>&by=date&sort=<?=$sort?>">Date</a></th>
                            </tr>
                        </thead>
                        <tbody>
                        
                            <?php 
                            
                            $startrow            = $getp * $page_limit;
                            $calculateendrow    = $startrow + $page_limit;
                            
                            if        ($calculateendrow > $filecount) $endrow = $filecount + 1;
                            else    $endrow = $startrow + $page_limit;
                            
                        //    for ($i = 0; $i <= $filecount; $i++) { Use this to view all the downloads on one page
                            for ($i = $startrow; $i <= $endrow - 1; $i++) {
                                
                                $rowbg = $rowbg == 'even' ? 'odd' : 'even';
                                
                            ?>
                
                                 <tr class="<?=$rowbg?>">
                                    <td class="tcl"><a href="download.php?d=<?=$category.",".rawurlencode($files[$i]['filename']).",".$files[$i]['filesize']?>" title="<?=htmlspecialchars($files[$i]['filenamenoext'])?>"><?=htmlspecialchars(substrdot($files[$i]['filenamenoext']))?></a></td>
                                    <td class="tc2" style="width:15%"><?=fsize($files[$i]['filesize'])?></td>
                                    <td class="tc3" style="width:15%"><a href="http://www.fileinfo.net/extension/<?=strtolower($files[$i]['filetype'])?>" target="_blank"><?=strtolower($files[$i]['filetype'])?></a></td>
                                    <td class="tcr" style="width:24%"><?=format_time($files[$i]['filedate'],true)?></td>
                                </tr>
                                
                            <?php } ?>
                            
                            
                        </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <?php $calculatepages = ceil($cfilecount / $page_limit) - 1; if ($calculatepages >= 1) { ?>
            
            <div class="linkst block" style="margin-top: 15px">
                <p class="pagelink">Pages:
                <?php
            
                    for ($j = 0; $j <= $calculatepages; ++$j) {
                        $k = $j + 1;
                        
                        $bold = $_GET['p'] == $j ? ' style="font-weight:bold"' : '';
                        
                        echo '<a'.$bold.' href="download.php?c='.$category.'&p='.$j.'&by='.$by.'&sort='.$sortp.'">'.$k.'</a> ';
                    }
                
                ?></p>
            </div>
        
        <?php } } ?>
    
    <?php } else { 
        
        // Get information file
        if(file_exists($dldir.'/.dldesc')) {
            
            require PUN_ROOT.'include/parser.php'; ?>
            
            <div class="blockform">    
                <h2><span>Information</span></h2>
                <div class="box">
                    <div class="inbox" style="padding:5px">
                        <?=parse_message(file_get_contents($dldir.'/.dldesc'), 0)?>
                    </div>
                </div>
            </div>
            <br />
            
        <?php } ?>
    
            <div class="blockform">
                <h2><span>File search</span></h2>
                <div class="box">
                <form id="filelist" method="post" action="download.php?c=<?=$category?>">
            
                    <input type="hidden" name="form_sent" value="1" />
                
                    <div class="inform">
                        <fieldset>
                            <legend>Find files</legend>
                            <div class="infldset">
                                <label class="conl">Filename<br /><input type="text" name="searchword" value="<?php if (isset($_SESSION['search'])) echo $_SESSION['search']['searchword']; ?>" size="25" maxlength="255" /><br /></label>
                                
                                <label class="conl">Search by
                                <br /><select name="searchby">
                                        <option value="1">Filename</option>
                                        <option value="2">Extension</option>
                                </select>
                                <br /></label>
                                
                                <label class="conl">Category
                                <br /><select name="category">
                                        <option value="">Select a category</option>
                                        <?php foreach ($dirsmenu as $menuitem) { echo '<option value="'.$menuitem.'">'.str_replace('_',' ',$menuitem).'</option>'."\n"; } ?>
                                </select>
                                <br /></label>
                                <p class="clearb">Enter a filename to search or search by extension/filetype.</p>
                            </div>
                        </fieldset>
                    </div>
                    <p><input type="submit" name="search" value="Submit" accesskey="s" /> <?php if (isset($_SESSION['search'])) echo '<a href="download.php?c='.$category.'&clean">View normal filelist.</a>'; ?></p>
                </form>
            
                </div>
            </div>
            <br />
        
        <div class="block">
            <h2><span>Downloads - <?=$ccount+1?> Categories</span></h2>
            <div class="box">
                <div class="inbox" style="padding:0">
        
                    <table cellspacing="0">
                    <thead>
                        <tr>
                            <th class="tcl" scope="col"><a href="download.php?by=name&sort=<?=$sort?>">Name</a></th>
                            <th class="tc2" scope="col" style="width:24%;text-align:left"><a href="download.php?by=date&sort=<?=$sort?>">Date</a></th>
                        </tr>
                    </thead>
                    <tbody>
                
                        <?php for ($i = 0; $i <= $ccount; $i++) { $rowbg = $rowbg == 'even' ? 'odd' : 'even'; ?>
            
                             <tr class="<?=$rowbg?>">
                                <td class="tcl"><a href="download.php?c=<?=$cdirs[$i]['cname']?>"><?=str_replace('_',' ',$cdirs[$i]['cname'])?></a></td>
                                <td class="tc2" style="width:24%;text-align:left"><?=date('F d Y', $cdirs[$i]['cdate'])?></td>
                            </tr>
                            
                        <?php } ?>
                        
                    </tbody>
                    </table>
                </div>
            </div>
        </div>
    
    <?php } ?>
    
    <div class="clearer"></div>    
</div>

<?php 
require PUN_ROOT.'footer.php';

I tope someone can help me.

Re: "Cannot modify header information" problem

Sounds like you have some whitespace before the <?php tag

http://fluxbb.org

Free PunBB Hosting - lots of mods, easy to customize

3

Re: "Cannot modify header information" problem

Couldn't find it. But after replacing the files with those from my local forum is worked again.

Re: "Cannot modify header information" problem

Probably an UTF-8 character that sneaked in.