4 * Model of an Attachment
6 class MimeMailParser_attachment
{
9 * @var $filename Filename
13 * @var $content_type Mime Type
17 * @var $content File Content
21 * @var $extension Filename extension
25 * @var $content_disposition Content-Disposition (attachment or inline)
27 public $content_disposition;
29 * @var $headers An Array of the attachment headers
35 public function __construct($filename, $content_type, $stream, $content_disposition = 'attachment', $headers = array()) {
36 $this->filename
= $filename;
37 $this->content_type
= $content_type;
38 $this->stream
= $stream;
39 $this->content
= null;
40 $this->content_disposition
= $content_disposition;
41 $this->headers
= $headers;
45 * retrieve the attachment filename
48 public function getFilename() {
49 return $this->filename
;
53 * Retrieve the Attachment Content-Type
56 public function getContentType() {
57 return $this->content_type
;
61 * Retrieve the Attachment Content-Disposition
64 public function getContentDisposition() {
65 return $this->content_disposition
;
69 * Retrieve the Attachment Headers
72 public function getHeaders() {
73 return $this->headers
;
77 * Retrieve the file extension
80 public function getFileExtension() {
81 if (!$this->extension
) {
82 $ext = substr(strrchr($this->filename
, '.'), 1);
84 // special case, tar.gz
85 // todo: other special cases?
86 $ext = preg_match("/\.tar\.gz$/i", $ext) ?
'tar.gz' : 'gz';
88 $this->extension
= $ext;
90 return $this->extension
;
94 * Read the contents a few bytes at a time until completed
95 * Once read to completion, it always returns false
97 * @param $bytes Int[optional]
99 public function read($bytes = 2082) {
100 return feof($this->stream
) ?
false : fread($this->stream
, $bytes);
104 * Retrieve the file content in one go
105 * Once you retrieve the content you cannot use MimeMailParser_attachment::read()
108 public function getContent() {
109 if ($this->content
=== null) {
110 fseek($this->stream
, 0);
111 while(($buf = $this->read()) !== false) {
112 $this->content
.= $buf;
115 return $this->content
;
119 * Allow the properties
120 * MimeMailParser_attachment::$name,
121 * MimeMailParser_attachment::$extension
122 * to be retrieved as public properties
123 * @param $name Object
125 public function __get($name) {
126 if ($name == 'content') {
127 return $this->getContent();
128 } else if ($name == 'extension') {
129 return $this->getFileExtension();