Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

official media types at <http://www.isi. edu/in-
notes/iana/assignments/media-types/ >.


At times, you may wish to take advantage of how browsers react to different types of
content. For example, text/plain displays in a fixed-width font with no interpretation
of HTML. If you use / for the content type, the browser displays a dialog window for
saving the file. Perhaps the most interesting use is for launching a helper application.


Listing 18.5 creates a tab-delimited text file that may launch Microsoft Excel. Take
note that the computer must meet a few qualifications, however. First, it probably needs
to be running Windows, and it must have Microsoft Excel installed. Newer versions of
Excel associate the application/ vnd.ms-excel content type with .xls files.
My experience has been that these headers will cause an Excel OLE container inside
either MSIE or Netscape Navigator on a Windows machine, but your mileage may vary.
Other browsers will likely ask the user if the file should be saved.


Notice the second header in Listing 18.5, Content-Disposition. This is not part
of the HTTP 1.1 standard but is widely implemented. It allows you to suggest a file
name. If you add attachment; to the header, the browser may choose to open Excel in
a separate window.


Using Content-Type this way is almost black magic, since browsers don't follow a
standard when encountering different MIME types. This technique has proven to be most
successful for me when writing intranet applications where I had the luxury of serving a
narrow set of browsers.


Email with Attachments


Sending plain email with PHP is easy. The mail function handles all the messy protocol
details behind the scenes. But if you want to send attachments, you will need to dig into
an RFC, specifically RFC 1341. This RFC describes MIME, Multipurpose Internet Mail
Extensions. You can read it at the faqs.org site
http://www.faqs.org/rfcs/rfc1341.html, but I'll show you a somewhat naòve
implementation.


Listing 18.5 Sending a Tab-Delimited Excel File

Free download pdf