Page 1 of 2

Problem for downloading a file

Posted: Thu Nov 06, 2003 6:23 pm
by morpheus2matrix
Hi MOD-Authors,



For my Log Actions MOD, i want to add a download logs features. So here is the code i'm using :

Code: Select all

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

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

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-Disposition: attachment; filename=' . $filename);
		header('Content-Description: File Transfert');
		header('Pragma: no-cache');
		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;
	}

But the result of this code is the log selected appear on the screen, and they are not proposed to be downloaded.

Any ideas ? More details wanted ?


Thanks for reading that. :)

Posted: Thu Nov 06, 2003 9:50 pm
by A_Jelly_Doughnut
This behavior should be dependant on the MIME type of the file you are sending. I don't know if there is a PHP function to change the MIME type, but it should be application/plain-text for the download to run, IIRC.

Posted: Thu Nov 06, 2003 10:37 pm
by morpheus2matrix
I'm trying to download a .cvs file so i've used :

Code: Select all

header('Content-Type: application/csv; name=\'' . $filename . '\'');

Posted: Mon Nov 10, 2003 8:59 am
by morpheus2matrix
bump :)

Posted: Mon Nov 10, 2003 7:17 pm
by Mohd
try this :D

Code: Select all

application/force-download

Posted: Mon Nov 10, 2003 7:20 pm
by not radio

Code: Select all

header ( 'Content-Disposition: attachment; filename=' . $filename );

Posted: Mon Nov 10, 2003 7:25 pm
by morpheus2matrix
not radio wrote:

Code: Select all

header ( 'Content-Disposition: attachment; filename=' . $filename );


I've got the same result (btw, i don't see the difference with what i've written)

Posted: Mon Nov 10, 2003 7:26 pm
by Mohd
Mohd wrote: try this :D

Code: Select all

application/force-download

Posted: Tue Nov 11, 2003 12:04 pm
by morpheus2matrix
Mohd wrote:
Mohd wrote:try this :D

Code: Select all

application/force-download


still same result :cry:

Posted: Tue Nov 11, 2003 6:38 pm
by Silvertongue
Don't you need some type of "Refresh Content" command in the header information? I'm not all that familiar with how the auto-download stuff works, but I'm learning.

I just looked at the source code for how the mod-db starts the download automatically, and it has this line at the end:

Code: Select all

<meta http-equiv="refresh" content="5;url=http://www.phpbb.com/phpBB/uploads/mods/EasyMOD_a3_0-0-10a.zip">
So, wouldn't something like that be needed in your code, after the file has been established?

Posted: Tue Nov 11, 2003 6:41 pm
by morpheus2matrix
this is a good solution if the file already exist on the server. But i'im trying to force the download of a file which is not on it : it's created and then downloaded.

Posted: Tue Nov 11, 2003 6:42 pm
by Silvertongue
Ok....but why not have the file saved on the server once you've generated it? Have an "archive" directory or something. Btw, I'm particularly interested in this because I happen to use the Log Actions mod at my forum.

I *do* think having an archive or something would be helpful...

Posted: Tue Nov 11, 2003 9:27 pm
by morpheus2matrix
Silvertongue wrote: Have an "archive" directory or something.


I agree but some werver won't allow to create files throught PHP so that's why i prefer the solution i'm trying to add :lol:

Posted: Wed Nov 12, 2003 6:30 pm
by Silvertongue
I'm still doing some research on this one, 'cause now I'm interested in how it turns out. LOL I'll post some of the things that I've found that might be options...

Code: Select all

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$myfilename);
header("Expires: Mon, 25 Aug 2003 12:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
I've actually seen this in several places now.....from what I've been able to tell, try removing the

Code: Select all

name=\'' . $filename . '\'
from the content type line.

Posted: Wed Nov 12, 2003 6:51 pm
by morpheus2matrix
Silvertongue wrote: I'm still doing some research on this one, 'cause now I'm interested in how it turns out. LOL I'll post some of the things that I've found that might be options...

Code: Select all

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$myfilename);
header("Expires: Mon, 25 Aug 2003 12:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
I've actually seen this in several places now.....from what I've been able to tell, try removing the

Code: Select all

name=\'' . $filename . '\'
from the content type line.


Still same result :
Headers already sent.......


:cry: