1: <?php
2:
3: namespace PHPixie\HTTP\Messages\UploadedFile;
4:
5: use PHPixie\HTTP\Messages;
6: use RuntimeException;
7:
8: 9: 10:
11: class Implementation extends \PHPixie\HTTP\Messages\UploadedFile
12: {
13: 14: 15: 16: 17: 18: 19: 20: 21:
22: public function __construct(
23: $messages,
24: $file,
25: $clientFilename = null,
26: $clientMediaType = null,
27: $size = null,
28: $error = UPLOAD_ERR_OK
29: )
30: {
31: parent::__construct($messages);
32:
33: $this->file = $file;
34: $this->clientFilename = $clientFilename;
35: $this->clientMediaType = $clientMediaType;
36: $this->size = $size;
37: $this->error = $error;
38: }
39:
40: 41: 42:
43: public function getClientFilename()
44: {
45: if($this->clientFilename === null) {
46: $this->clientFilename = $this->getFileBasename();
47: }
48:
49: return $this->clientFilename;
50: }
51:
52: 53: 54:
55: public function getClientMediaType()
56: {
57: if($this->clientMediaType === null) {
58: if(($type = $this->getFileType()) === false) {
59: throw new RuntimeException("Could not determine the file type of '{$this->file}'");
60: }
61:
62: $this->clientMediaType = $type;
63: }
64:
65: return $this->clientMediaType;
66: }
67:
68: 69: 70:
71: public function getSize()
72: {
73: if($this->size === null) {
74: if(($size = $this->getFileSize()) === false) {
75: throw new RuntimeException("Could not determine the file size of '{$this->file}'");
76: }
77:
78: $this->size = $size;
79: }
80:
81: return $this->size;
82: }
83:
84: 85: 86:
87: public function moveTo($path)
88: {
89: $this->assertValidUpload();
90: if(!$this->moveFile($path)) {
91: throw new RuntimeException("Failed to move uploaded file '{$this->file}' to '$path'");
92: }
93: }
94:
95: 96: 97: 98:
99: protected function moveFile($path)
100: {
101: return rename($this->file, $path);
102: }
103:
104: 105: 106:
107: protected function getFileBasename()
108: {
109: return pathinfo($this->file, PATHINFO_BASENAME);
110: }
111:
112: 113: 114:
115: protected function getFileType()
116: {
117: return filetype($this->file);
118: }
119:
120: 121: 122:
123: protected function getFileSize()
124: {
125: return filesize($this->file);
126: }
127: }