Advertisement:

Author Topic: View .txt files instead of Saving them.  (Read 24083 times)

Offline JesseTyler

  • Newbie
  • *
  • Posts: 3
View .txt files instead of Saving them.
« on: April 20, 2005, 04:22:18 PM »
This would be REALLY helpful on a SMF forum i'm making. (Dealing with tons of txt files.)

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: View .txt files instead of Saving them.
« Reply #1 on: April 20, 2005, 09:56:01 PM »
Why can't you just click "open" instead of "save"?

I can tell you what to change, though.

-[Unknown]

Offline JesseTyler

  • Newbie
  • *
  • Posts: 3
Re: View .txt files instead of Saving them.
« Reply #2 on: April 21, 2005, 01:09:35 AM »
Ok look.
I'd want it to be able to just attach the .txt file to the post. Then click on it and have it open like i'm viewing a .txt file directly. :p

Offline Benta

  • Semi-Newbie
  • *
  • Posts: 11
Re: View .txt files instead of Saving them.
« Reply #3 on: April 22, 2005, 07:52:44 PM »
Ok look.
I'd want it to be able to just attach the .txt file to the post. Then click on it and have it open like i'm viewing a .txt file directly. :p

Right-clicking doesn't work?

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: View .txt files instead of Saving them.
« Reply #4 on: April 22, 2005, 08:06:12 PM »
Alright, find this in Display.php:

Code: [Select]
header('Content-Disposition: attachment; filename="' . $real_filename . '"');
header('Content-Type: application/octet-stream');

Replace with:

Code: [Select]
header('Content-Disposition: ' . (substr($real_filename, -4) === '.txt' ? 'inline' : 'attachment') . '; filename="' . $real_filename . '"');
if (substr($real_filename, -4) === '.txt')
header('Content-Type: text/plain');
else
header('Content-Type: application/octet-stream');

But, I still think the browser has more elegant support for this.  Meaning, if I click on an attachment WITHOUT making the above change, I get a dialog that gives me the option to "Open" or "Save" the document.  Choosing "Open" opens it in Notepad, which I like to use to view text files and even prefer to my web browser.

This change cannot make it automatically pick "Open" and use Notepad, and such a thing is not going to be possible from a webpage.  However, it will make the web browser simply display the file instead of downloading it.

-[Unknown]

Offline Elijah Bliss

  • Sophist Member
  • *****
  • Posts: 1,340
Re: View .txt files instead of Saving them.
« Reply #5 on: April 22, 2005, 09:13:48 PM »
Alright, find this in Display.php:

Code: [Select]
header('Content-Disposition: attachment; filename="' . $real_filename . '"');
header('Content-Type: application/octet-stream');

Replace with:

Code: [Select]
header('Content-Disposition: ' . (substr($real_filename, -4) === '.txt' ? 'inline' : 'attachment') . '; filename="' . $real_filename . '"');
if (substr($real_filename, -4) === '.txt')
header('Content-Type: text/plain');
else
header('Content-Type: application/octet-stream');

But, I still think the browser has more elegant support for this.  Meaning, if I click on an attachment WITHOUT making the above change, I get a dialog that gives me the option to "Open" or "Save" the document.
-[Unknown]

Not in Safari.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: View .txt files instead of Saving them.
« Reply #6 on: April 22, 2005, 09:39:01 PM »
So use a better browser :P.

-[Unknown]

Offline Elijah Bliss

  • Sophist Member
  • *****
  • Posts: 1,340
Re: View .txt files instead of Saving them.
« Reply #7 on: April 22, 2005, 09:49:03 PM »
So use a better browser :P.

-[Unknown]

Hey, Safari rules! lol. You never read a complaint from me about poor rendering in Safari, actually, that's a big reason why I like SMF, it looks really good Safari.

Offline JesseTyler

  • Newbie
  • *
  • Posts: 3
Re: View .txt files instead of Saving them.
« Reply #8 on: April 23, 2005, 10:38:36 PM »
Thanks Unknown. It works perfectly now. :D