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