MyBB Community Forums

Full Version: When user click on attachment, how to force them to download it, not view in browser?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
You know, for text files and stuff, it just opens a new page in the browser when you try to download it. How can you make it so that all the files (including text files) have to be downloaded and not viewed in the browser? Because I'm having problems with other file extensions like .vb and .cs and it displays the code in the browser and I rather have users download them...
I just wrote a very small plugin to do that.

Download the file, move it to the inc/plugins/ folder of your MyBB installation and activate the plugin in the AdminCP.
Didn't MyBB implement something like this before?
(2011-04-21, 12:19 PM)faviouz Wrote: [ -> ]Didn't MyBB implement something like this before?

As far as I can see is that MyBB checks the content type and if it is an image or text file, it will automatically show it inline (if the browser supports that):

// -- snippet --
	switch($attachment['filetype'])
	{
		case "application/pdf":
		case "image/bmp":
		case "image/gif":
		case "image/jpeg":
		case "image/pjpeg":
		case "image/png":
		case "text/plain":
			header("Content-type: {$attachment['filetype']}");
			$disposition = "inline";
			break;

		default:
			header("Content-type: application/force-download");
			$disposition = "attachment";
	}

	if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "msie") !== false)
	{
		header("Content-disposition: attachment; filename=\"{$attachment['filename']}\"");
	}
	else
	{
		header("Content-disposition: {$disposition}; filename=\"{$attachment['filename']}\"");
	}
// -- /snippet --
Oh yeah, that was the implementation I was talking about.

Nice plugin by the way.
right click -- save as
Yeah the right click save as would work obviously, but when you have files like .vb and .cs, nobody would think of right click save as those type of files unless you saw .txt then yeah if you wanted to download it you'd do that.

Oh and thank you Aries!

I have just one concern:

This doesn't seem to be working for my Download Manager plugin. It works for the forum in general, but not for this downoads manager plugin I have. I have this plugin called "MyDownloads" by Pirata Nervo and its a basic downloads system for mybb and when my users submit .vb/.cs/.txt files to the download system they are opened via browser and you can't right click save-as because in Pirata Nervo's plugin there is no link, it is a download button and you can't right click a download button. Is there anyway to make the MyDownloads plugin apply the same rules as the forum rules for downloading attachments?
(2011-04-22, 05:06 AM)zero_coke Wrote: [ -> ]This doesn't seem to be working for my Download Manager plugin. It works for the forum in general, but not for this downoads manager plugin I have. I have this plugin called "MyDownloads" by Pirata Nervo and its a basic downloads system for mybb and when my users submit .vb/.cs/.txt files to the download system they are opened via browser and you can't right click save-as because in Pirata Nervo's plugin there is no link, it is a download button and you can't right click a download button. Is there anyway to make the MyDownloads plugin apply the same rules as the forum rules for downloading attachments?

I don't know how MyDownloads handles its downloads and as it is a paid plugin I can't check it out for you.
Thank for the info
________________________________________________
For the atmosphere. ผลบอล in the drama, Thailand, mostly from observations of the atmosphere ผลบอล Poipet sure enough.