Jump to content

Wrong Mimetype Output (HTML Doc instead of rar or zip)


Irap

Recommended Posts

The file being outputted is a .rar

 

	# $file['filename'] = "myfile.rar"; // given by database

	$file['extension'] = substr( $file['filename'], -3 );

	// Set the mime according to the extension.
	// I believe these are appropriate mime types to use.
	if ( $file['extension'] == "rar" )
	{
		$file['mime'] = "application/x-rar-compressed";
	}
	elseif ( $file['extension'] == "zip" )
	{
		$file['mime'] = "application/zip";
	}

	// Added the if statement just in case the above wasn't working.
	if ( $file['mime'] == "application/zip" || $file['mime'] == "application/x-rar-compressed" )
	{
		mysql_query("
			UPDATE `web_downloads_mods`
			SET `downloads` ='{$file_mod['file_downloads']}'
			WHERE `id` ='{$file['id']}'
			LIMIT 1
		");

		@header( "Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT" ); # expire the file in 2 hours.
		@header( "Last-Modified: " . gmdate("D, d M Y H:i:s", $file['uploaded']) . " GMT" );
		@header( "Content-Length: {$file['filesize']}" ); # given by the database in bytes.
		@header( "Content-Disposition: attachment; filename='". $file['filename'] ."'" );
		// Internet Explorer compatibility:
		@header( "Pragma: " );
 		@header( "Cache-Control: " );

		readfile( "http://". $_SERVER['HTTP_HOST'] ."/uploads/". $file['filename'] );
	}

 

Firefox will open the file in a new tab and display gibberish...

 

Except, of course... At the beginning of the file there is a fun "Rar!". Is this something that occurs in many files and can be used to test if files are the right extension?

You need to specify a Content-Type header.

 

@header("Content-Type: {$file['mime']}");

 

Edit: On that note, why are you suppressing errors from the header() calls? They won't output any errors unless you already sent content data to the browser.

Heh, I thought I had used content type.

 

 

The problem with PHP outputting errors is that it messes up the file that is being printed. As they say, don't output anything else besides from the file data when using header() to output files... Otherwise you get an error at the start of the file.

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.