1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Jlab\Eloglib;
10:
11:
12: class FileAttachment extends Attachment
13: {
14:
15:
16: 17: 18: 19: 20: 21: 22:
23: function __construct($filename, $caption = '', $type = '')
24: {
25: $this->encoding = 'base64';
26: $this->readFromFile($filename);
27: $this->setMimeType($filename, $type);
28: $this->caption = $caption;
29: }
30:
31: 32: 33: 34: 35: 36: 37:
38: protected function readFromFile($filename){
39: $data = file_get_contents($filename);
40: if ($data === false) {
41: throw new IOException("Unable to read contents of attachment file");
42: }
43: $this->data = base64_encode($data);
44: $this->filename = basename($filename);
45: }
46:
47: 48: 49: 50: 51:
52: protected function setMimeType($filename, $type)
53: {
54: if ($type) {
55: $this->type = $type;
56: } else {
57:
58: $finfo = finfo_open(FILEINFO_MIME_TYPE);
59: $this->type = finfo_file($finfo, $filename);
60: }
61: }
62:
63: }