Problem for downloading a file

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

Ok, another variation that I've seen....slightly different coding style used...

Code: Select all

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=' . $filename");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

nop, still same result :?

I think the problem should not be here, the header are right (i think).

I give you the code of the entire file, maybe you will be able to find a solution :

Code: Select all

<?php
/***************************************************************************
 *				  admin_logs.php
 *                              -------------------
 *     begin                : Jan 24 2003
 *     copyright            : Morpheus
 *     email                : morpheus@2037.biz
 *
 *     $Id: admin_logs.php,v 1.85.2.9 2003/01/24 18:31:54 Moprheus Exp $
 *
 ****************************************************************************/

/***************************************************************************
 *
 *   This program 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.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);


if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Logs']['Logs Actions'] = "$file";
	return;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);


$template->set_filenames(array(
	"body" => "admin/logs_body.tpl")
);

$start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;

if ( isset($HTTP_POST_VARS['order']) )
	{
		$sort_order = ($HTTP_POST_VARS['order'] == 'ASC') ? 'ASC' : 'DESC';
	}
else if ( isset($HTTP_GET_VARS['order']) )
	{
		$sort_order = ($HTTP_GET_VARS['order'] == 'ASC') ? 'ASC' : 'DESC';
	}
else
	{
		$sort_order = 'ASC';
	}

if ( file_exists($phpbb_root_path . 'install_tables.' . $phpEx) )
{
	message_die(GENERAL_MESSAGE, 'You have not yet delete the file install_tables.php : do it before trying to see this page.');
}

$sql = "SELECT config_value AS all_admin
FROM " . LOGS_CONFIG_TABLE . "
WHERE config_name = 'all_admin' ";

if(!$result = $db->sql_query($sql)) 
{ 
   message_die(CRITICAL_ERROR, "Could not query log config informations", "", __LINE__, __FILE__, $sql); 
}
$row = $db->sql_fetchrow($result);
$all_admin_authorized = $row['all_admin'];

if ( $all_admin_authorized == '0' && $userdata['user_id'] <> '2' && $userdata['user_view_log'] <> '1' )
{
	message_die(GENERAL_MESSAGE, $lang['Admin_not_authorized']);
}

//
// Logs sorting
//

$mode_types_text = array('Time', 'Member', 'Action', 'Id');
$mode_types = array('time', 'username', 'mode', 'id');
	
$select_sort_mode = '<select name="mode">';
for($i = 0; $i < count($mode_types_text); $i++)
	{
		$selected = ( $mode == $mode_types[$i] ) ? ' selected="selected"' : '';
		$select_sort_mode .= "<option value=\"" . $mode_types[$i] . "\"$selected>" . $mode_types_text[$i] . "</option>";
	}
$select_sort_mode .= '</select>';
	
$select_sort_order = '<select name="order">';
if($sort_order == 'ASC')
	{
		$select_sort_order .= '<option value="ASC" selected="selected">' . $lang['Sort_Ascending'] . '</option><option value="DESC">' . $lang['Sort_Descending'] . '</option>';
	}
else
	{
		$select_sort_order .= '<option value="ASC">' . $lang['Sort_Ascending'] . '</option><option value="DESC" selected="selected">' . $lang['Sort_Descending'] . '</option>';
	}
$select_sort_order .= '</select>';
	

$template->assign_vars(array(
	'L_LOG_ACTIONS_TITLE' => $lang['Log_action_title'],
	'L_LOG_ACTION_EXPLAIN' => $lang['Log_action_explain'],
	'L_CHOOSE_SORT' => $lang['Choose_sort_method'],
	'L_ORDER' => $lang['Order'],
	'L_GO' => $lang['Go'],
	'L_CANCEL' => $lang['Cancel'],
	'L_DELETE' => $lang['Delete'], 
	'L_DELETE_LOG' => $lang['Choose_log'],
	'L_ID_LOG' => $lang['Id_log'],
	'L_ACTION' => $lang['Action'],
	'L_TOPIC' => $lang['Topic'],
	'L_DONE_BY' => $lang['Done_by'],
	'L_USER_IP' => $lang['User_ip'],
	'L_DATE' => $lang['Date'],
	'L_MARK_ALL' => $lang['Select_all'],
	'L_UNMARK_ALL' => $lang['Unselect_all'],

	'S_MODE_SELECT' => $select_sort_mode,
	'S_ORDER_SELECT' => $select_sort_order,
	'S_MODE_ACTION' => append_sid("admin_logs.$phpEx"),
	'S_CANCEL_ACTION' => append_sid("admin_logs.$phpEx"))
);


if ( isset($HTTP_GET_VARS['mode']) || isset($HTTP_POST_VARS['mode']) )
{
	$mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];

	switch( $mode )
	{
		case 'mode' :
			$order_by = "mode $sort_order LIMIT $start, " . $board_config['topics_per_page'];
			break;
		case 'username' :
			$order_by = "username $sort_order LIMIT $start, " . $board_config['topics_per_page'];
			break;
		case 'time' :
			$order_by = "time $sort_order LIMIT $start, " . $board_config['topics_per_page'];
			break;
		case 'id' :
			$order_by = "id_log $sort_order LIMIT $start, " . $board_config['topics_per_page'];
			break;
		default:
			$order_by = "time DESC LIMIT $start, " . $board_config['topics_per_page'];
			break;
	}
}
else
	{
		$order_by = "time DESC LIMIT $start, " . $board_config['topics_per_page'];
	}

	$sql = "SELECT * 
		FROM " . LOGS_TABLE . "
		ORDER BY $order_by "; 
		if(!$result = $db->sql_query($sql)) 
		{ 
		   message_die(CRITICAL_ERROR, "Could not query log informations", "", __LINE__, __FILE__, $sql); 
		} 
		$rows = $db->sql_fetchrowset($result); 
		$numrows = $db->sql_numrows($result); 
		for ($i = 0; $i < $numrows; $i++) 
		{
			$id_log = $rows[$i]['id_log'];
			$action = ucfirst($rows[$i]['mode']); 
			$topic = $rows[$i]['topic_id']; 
			$user_id = $rows[$i]['user_id']; 
			$username = $rows[$i]['username'];
			$user_ip = decode_ip($rows[$i]['user_ip']);
			$date = $rows[$i]['time']; 
		
		$sql = "SELECT user_level
			FROM " . USERS_TABLE . "
			WHERE user_id = $user_id";
		
		if(!$result = $db->sql_query($sql)) 
		{ 
		   message_die(CRITICAL_ERROR, "Could not query user_level informations", "", __LINE__, __FILE__, $sql); 
		} 
		$row = $db->sql_fetchrow($result);
		$level = $row['user_level'];

		$style_color = '';
		if ( $level == ADMIN ) 
		{
			$style_color = 'style="color:#' . $theme['fontcolor3'] . '"';
		}
		else if ( $level == MOD )
		{
			$style_color = 'style="color:#' . $theme['fontcolor2'] . '"';
		}

	    $temp_url = append_sid('admin_users.'.$phpEx.'?mode=edit&u=' . $user_id); 
	    $temp2_url = append_sid($phpbb_root_path .'viewtopic.'.$phpEx.'?t=' . $topic);


		 $template->assign_block_vars('record_row', array( 
		    'ID_LOG' => $id_log,
		    'U_ACTION' => append_sid($phpbb_root_path . 'includes/rules.' . $phpEx . '?mode=' . $action),
		    'ACTION' => $action,
		    'TOPIC' => $topic, 
		    'TOPIC_IMG' => '<a href="' . $temp2_url . '" target="_blank"><img src = "' .$phpbb_root_path . $images['icon_latest_reply']. '" title="' . $lang['See_topic'] . '" alt="' . $lang['See_topic'] . '" border="0"></a>', 
		    'USER_ID' => $user_id, 
		    'USERNAME' => '<a href="' . $temp_url . '"' . $style_color .' target=_new>' . $username . '</a>', 
		    'USER_IP' => $user_ip,
		    'U_WHOIS_IP' => 'http://network-tools.com/default.asp?prog=express&Netnic=whois.arin.net&host=' . $user_ip, 
		    'DATE' => create_date($board_config['default_dateformat'], $date, $board_config['board_timezone'])) 
		 );
	}
$db->sql_freeresult($result);


$log_list = ( isset($HTTP_POST_VARS['log_list']) ) ?  $HTTP_POST_VARS['log_list'] : array();
$delete = ( isset($HTTP_POST_VARS['delete']) ) ?  TRUE : FALSE ;

$log_list_sql = implode(', ', $log_list);

$download = ( isset($HTTP_POST_VARS['download']) || isset($HTTP_GET_VARS['download']) ) ?  TRUE : FALSE ;

$mode = '';
if ( $download )
{
	$mode = "csv";
}

if ( $log_list_sql != '' )
{
	if ( $delete )
	{
		$sql = "DELETE 
		FROM " . LOGS_TABLE . " 
		WHERE id_log IN (" . $log_list_sql . ")";

		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not delete Logs', '', __LINE__, __FILE__, $sql);
		}
		else
		{
			$redirect_page = append_sid("admin_logs.$phpEx");
			$l_redirect = sprintf($lang['Click_return_admin_log'], '<a href="' . $redirect_page . '">', '</a>');

			message_die(GENERAL_MESSAGE, $lang['Log_delete'] . '<br /><br />' . $l_redirect);
		}
	}
	if( $mode == "csv" )
	{
		$today = date("Ymd");
		$filename = "Log_actions_" . $today . ".csv";

		//@ob_start();
		//@ob_implicit_flush(0);

		header('Content-Type: application/csv; name=\'' . $filename . '\'');
		header('Content-Type: application/force-download');
		header('Content-Transfer-Encoding: fichier');
		header('Content-Disposition: attachment; filename=' . $filename);
		header('Content-Description: File Transfert');
		header('Pragma: no-cache');
		header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); 
		header('Expires: 0');

		$sql = "SELECT * 
		FROM " . LOGS_TABLE . "
		WHERE id_log IN (" . $log_list_sql . ")"; 

		if(!$result = $db->sql_query($sql)) 
		{ 
		   message_die(CRITICAL_ERROR, "Could not query log informations", "", __LINE__, __FILE__, $sql); 
		} 
		$list = $db->sql_fetchrowset($result); 
		
		$csv_output = '';
		for ($i = 0; $i < count($list); $i++) 
		{
			$csv_output .= $list[$i]['id_log'] . '; ' . ucfirst($list[$i]['mode']) . '; ' . $list[$i]['topic_id'] . '; ' . $list[$i]['username'] . '; ' . decode_ip($list[$i]['user_ip']) . '; ' . create_date($board_config['default_dateformat'], $list[$i]['time'], $board_config['board_timezone']) . '; ' . "\n";
		}

		//ob_end_clean();

		printf("%s", $csv_output);
		message_die(GENERAL_MESSAGE, 'Your download will start shortly, please be patient.');
		exit;
	}
}


if ( $board_config['topics_per_page'] > 10 )
{
	$sql = "SELECT count(*) AS total
		FROM " . LOGS_TABLE;
		if ( !($result = $db->sql_query($sql)) ) 
	   { 
	      message_die(GENERAL_ERROR, 'Error getting total informations for logs', '', __LINE__, __FILE__, $sql); 
	   }

	   if ( $total = $db->sql_fetchrow($result) ) 
	   { 
	      $total_records = $total['total']; 
	
	      $pagination = generate_pagination("admin_logs.$phpEx?mode=$mode&order=$sort_order", $total_records, $board_config['topics_per_page'], $start). '&'; 
	   } 
} 
else
	{
		$pagination = '&';
		$total_records = 10;
	}
	
	$template->assign_vars(array(
		'PAGINATION' => $pagination,
		'PAGE_NUMBER' => ( $total_records == '0' ) ? '&' : sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $total_records / $board_config['topics_per_page'] )), 	
		'L_GOTO_PAGE' => $lang['Goto_page'],
		'L_WHOSONLINE_ADMIN' => sprintf($lang['Admin_online_color'], '<span style="color:#' . $theme['fontcolor3'] . '">', '</span>'),
		'L_WHOSONLINE_MOD' => sprintf($lang['Mod_online_color'], '<span style="color:#' . $theme['fontcolor2'] . '">', '</span>'),)
	);

$template->pparse("body");

include('./page_footer_admin.'.$phpEx);

?>
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

Maybe I missed it, or didn't read the code properly, but I'm not seeing where you close the FOR loop here:

Code: Select all

      for ($i = 0; $i < $numrows; $i++) 
      { 
         $id_log = $rows[$i]['id_log']; 
         $action = ucfirst($rows[$i]['mode']); 
         $topic = $rows[$i]['topic_id']; 
         $user_id = $rows[$i]['user_id']; 
         $username = $rows[$i]['username']; 
         $user_ip = decode_ip($rows[$i]['user_ip']); 
         $date = $rows[$i]['time'];
User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

check here :

Code: Select all

}
$db->sql_freeresult($result);
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

LOL....i hate it when I miss little things like that. :oops:

I'll look at it again...
netclectic
Former Team Member
Posts: 4439
Joined: Wed Mar 13, 2002 3:08 pm
Location: Omnipresent
Contact:

Post by netclectic »

Make sure you don't have any spaces before the <? or after the ?>
Defend the game:
Image
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

Ok...just a couple of more things that I noticed, but I doubt it's the cause of the problem.

Code: Select all

$select_sort_mode = '<select name="mode">'; 
for($i = 0; $i < count($mode_types_text); $i++) 
   { 
      $selected = ( $mode == $mode_types[$i] ) ? ' selected="selected"' : ''; 
      $select_sort_mode .= "<option value=\"" . $mode_types[$i] . "\"$selected>" . $mode_types_text[$i] . "</option>"; 
   } 
$select_sort_mode .= '</select>';
I use similar select statements in a couple of things that I wrote, but here is how I have mine. Note the difference in adding the "selected" variable to the option.

Code: Select all

$select_sort_mode = '<select name="mode">'; 
for($i = 0; $i < count($mode_types_text); $i++) 
   { 
      $selected = ( $mode == $mode_types[$i] ) ? ' selected="selected"' : ''; 
      $select_sort_mode .= "<option value="' . $mode_types[$i] . '"' . $selected . '>" . $mode_types_text[$i] . "</option>"; 
   } 
$select_sort_mode .= '</select>';
The only other thing that I see, and this might just be how the code was copy and paste into the thread, is that there is some extra white-space after the closing PHP tag at the of the file. I can't count how many times that's happened to me, and I've beat my head against the wall trying to figure out why it was throwing an error. :?
User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

netclectic wrote: Make sure you don't have any spaces before the <? or after the ?>


already check : this is no the problem :cry:
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

Silvertongue wrote: Ok...just a couple of more things that I noticed, but I doubt it's the cause of the problem.


you're right, that's not the cause of the problem :wink:
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

Just found this post on a forum....not quite sure it would apply, but at this point, I'm not sure else the problem would be. :?

Hi there!
I have similar problem.
My headers are:
header("Expires: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Length: $size");
header("Content-Disposition: attachment; filename=\"$name\"");
header("Content-Transfer-Encoding: binary");
header("Content-Description: $name" );
And this does not allow to download the sent file.
BUT!
When i changed headers:
header('Cache-Control: private');
header ("Pragma: private");
everything worked!
But it seemed as it had caching.

If such problem had been solved already by someone please notify me by email or answer my post.
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

More information that I've found...

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Code: Select all

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>
Note: In PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini
User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

still no results :?


i think i will need to search for the function ob_start() and ob_end_flush() :?
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
Silvertongue
Registered User
Posts: 116
Joined: Tue Aug 20, 2002 4:31 am
Contact:

Post by Silvertongue »

User avatar
morpheus2matrix
Former Team Member
Posts: 9171
Joined: Wed Apr 10, 2002 7:31 pm
Location: France
Contact:

Post by morpheus2matrix »

Silvertongue wrote: http://us4.php.net/ob_start


I already know that, i just eed to work for adding this to the MOD :wink:
Former phpBB MOD-Team Member -

Forgive my bad English :(

No support by PM/Email - Thanks - You can thanks me here :) - Pay me for installing MOD's :lol:
Post Reply

Return to “[2.0.x] MOD Writers Discussion”