1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Input;
15:
16: /**
17: * Uploaded file.
18: *
19: * @category Jyxo
20: * @package Jyxo\Input
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Jakub Tománek
24: */
25: class Upload
26: {
27: /**
28: * Index in $_FILES superglobal array.
29: *
30: * @var string
31: */
32: private $index;
33:
34: /**
35: * The file was successfully uploaded.
36: *
37: * @var boolean
38: */
39: private $success = false;
40:
41: /**
42: * Constructor.
43: *
44: * @param string $index File index
45: */
46: public function __construct($index)
47: {
48: $this->index = $index;
49: }
50:
51: /**
52: * Confirms that the file was successfully uploaded.
53: *
54: * @return \Jyxo\Input\Upload
55: */
56: public function confirmUpload()
57: {
58: // Isset is just a simple check, it is not sufficient!
59: $this->success = isset($_FILES[$this->index]);
60: return $this;
61: }
62:
63: /**
64: * Returns file's temporary name.
65: *
66: * @return string
67: */
68: public function tmpName()
69: {
70: return isset($_FILES[$this->index]) ? $_FILES[$this->index]['tmp_name'] : null;
71: }
72:
73: /**
74: * Returns upload error type.
75: *
76: * @return integer
77: */
78: public function error()
79: {
80: return isset($_FILES[$this->index]) ? $_FILES[$this->index]['error'] : null;
81: }
82:
83: /**
84: * Moves the uploaded file.
85: *
86: * @param string $destination File destination
87: * @return boolean
88: */
89: public function move($destination)
90: {
91: $result = false;
92: if ($this->success) {
93: $result = move_uploaded_file($this->tmpName(), $destination);
94: }
95: return $result;
96: }
97:
98: /**
99: * Conversion to string because of other validators.
100: *
101: * @return string
102: */
103: public function __toString()
104: {
105: return $this->tmpName();
106: }
107:
108: /**
109: * Returns file extension.
110: *
111: * @return string
112: */
113: public function extension()
114: {
115: $ext = null;
116: if ($this->success) {
117: $ext = pathinfo($_FILES[$this->index]['name'], PATHINFO_EXTENSION);
118: }
119: return $ext;
120: }
121:
122: /**
123: * Returns file name.
124: *
125: * @return string
126: */
127: public function filename()
128: {
129: $filename = null;
130: if ($this->success) {
131: $filename = $_FILES[$this->index]['name'];
132: }
133: return $filename;
134: }
135: }
136: