|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\HttpFoundation\File; |
|
13 |
|
14 use Symfony\Component\HttpFoundation\File\Exception\FileException; |
|
15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
|
16 |
|
17 /** |
|
18 * A file uploaded through a form. |
|
19 * |
|
20 * @author Bernhard Schussek <bernhard.schussek@symfony.com> |
|
21 * @author Florian Eckerstorfer <florian@eckerstorfer.org> |
|
22 * @author Fabien Potencier <fabien@symfony.com> |
|
23 * |
|
24 * @api |
|
25 */ |
|
26 class UploadedFile extends File |
|
27 { |
|
28 /** |
|
29 * Whether the test mode is activated. |
|
30 * |
|
31 * Local files are used in test mode hence the code should not enforce HTTP uploads. |
|
32 * |
|
33 * @var Boolean |
|
34 */ |
|
35 private $test = false; |
|
36 |
|
37 /** |
|
38 * The original name of the uploaded file. |
|
39 * |
|
40 * @var string |
|
41 */ |
|
42 private $originalName; |
|
43 |
|
44 /** |
|
45 * The mime type provided by the uploader. |
|
46 * |
|
47 * @var string |
|
48 */ |
|
49 private $mimeType; |
|
50 |
|
51 /** |
|
52 * The file size provided by the uploader. |
|
53 * |
|
54 * @var string |
|
55 */ |
|
56 private $size; |
|
57 |
|
58 /** |
|
59 * The UPLOAD_ERR_XXX constant provided by the uploader. |
|
60 * |
|
61 * @var integer |
|
62 */ |
|
63 private $error; |
|
64 |
|
65 /** |
|
66 * Accepts the information of the uploaded file as provided by the PHP global $_FILES. |
|
67 * |
|
68 * The file object is only created when the uploaded file is valid (i.e. when the |
|
69 * isValid() method returns true). Otherwise the only methods that could be called |
|
70 * on an UploadedFile instance are: |
|
71 * |
|
72 * * getClientOriginalName, |
|
73 * * getClientMimeType, |
|
74 * * isValid, |
|
75 * * getError. |
|
76 * |
|
77 * Calling any other method on an non-valid instance will cause an unpredictable result. |
|
78 * |
|
79 * @param string $path The full temporary path to the file |
|
80 * @param string $originalName The original file name |
|
81 * @param string $mimeType The type of the file as provided by PHP |
|
82 * @param integer $size The file size |
|
83 * @param integer $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants) |
|
84 * @param Boolean $test Whether the test mode is active |
|
85 * |
|
86 * @throws FileException If file_uploads is disabled |
|
87 * @throws FileNotFoundException If the file does not exist |
|
88 * |
|
89 * @api |
|
90 */ |
|
91 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false) |
|
92 { |
|
93 if (!ini_get('file_uploads')) { |
|
94 throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path'))); |
|
95 } |
|
96 |
|
97 $this->originalName = basename($originalName); |
|
98 $this->mimeType = $mimeType ?: 'application/octet-stream'; |
|
99 $this->size = $size; |
|
100 $this->error = $error ?: UPLOAD_ERR_OK; |
|
101 $this->test = (Boolean) $test; |
|
102 |
|
103 if (UPLOAD_ERR_OK === $this->error) { |
|
104 parent::__construct($path); |
|
105 } |
|
106 } |
|
107 |
|
108 /** |
|
109 * Returns the original file name. |
|
110 * |
|
111 * It is extracted from the request from which the file has been uploaded. |
|
112 * Then is should not be considered as a safe value. |
|
113 * |
|
114 * @return string|null The original name |
|
115 * |
|
116 * @api |
|
117 */ |
|
118 public function getClientOriginalName() |
|
119 { |
|
120 return $this->originalName; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Returns the file mime type. |
|
125 * |
|
126 * It is extracted from the request from which the file has been uploaded. |
|
127 * Then is should not be considered as a safe value. |
|
128 * |
|
129 * @return string|null The mime type |
|
130 * |
|
131 * @api |
|
132 */ |
|
133 public function getClientMimeType() |
|
134 { |
|
135 return $this->mimeType; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Returns the file size. |
|
140 * |
|
141 * It is extracted from the request from which the file has been uploaded. |
|
142 * Then is should not be considered as a safe value. |
|
143 * |
|
144 * @return integer|null The file size |
|
145 * |
|
146 * @api |
|
147 */ |
|
148 public function getClientSize() |
|
149 { |
|
150 return $this->size; |
|
151 } |
|
152 |
|
153 /** |
|
154 * Returns the upload error. |
|
155 * |
|
156 * If the upload was successful, the constant UPLOAD_ERR_OK is returned. |
|
157 * Otherwise one of the other UPLOAD_ERR_XXX constants is returned. |
|
158 * |
|
159 * @return integer The upload error |
|
160 * |
|
161 * @api |
|
162 */ |
|
163 public function getError() |
|
164 { |
|
165 return $this->error; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Returns whether the file was uploaded successfully. |
|
170 * |
|
171 * @return Boolean True if no error occurred during uploading |
|
172 * |
|
173 * @api |
|
174 */ |
|
175 public function isValid() |
|
176 { |
|
177 return $this->error === UPLOAD_ERR_OK; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Moves the file to a new location. |
|
182 * |
|
183 * @param string $directory The destination folder |
|
184 * @param string $name The new file name |
|
185 * |
|
186 * @return File A File object representing the new file |
|
187 * |
|
188 * @throws FileException if the file has not been uploaded via Http |
|
189 * |
|
190 * @api |
|
191 */ |
|
192 public function move($directory, $name = null) |
|
193 { |
|
194 if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) { |
|
195 return parent::move($directory, $name); |
|
196 } |
|
197 |
|
198 throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname())); |
|
199 } |
|
200 |
|
201 /** |
|
202 * Returns the maximum size of an uploaded file as configured in php.ini |
|
203 * |
|
204 * @return type The maximum size of an uploaded file in bytes |
|
205 */ |
|
206 static public function getMaxFilesize() |
|
207 { |
|
208 $max = trim(ini_get('upload_max_filesize')); |
|
209 |
|
210 if ('' === $max) { |
|
211 return PHP_INT_MAX; |
|
212 } |
|
213 |
|
214 switch (strtolower(substr($max, -1))) { |
|
215 case 'g': |
|
216 $max *= 1024; |
|
217 case 'm': |
|
218 $max *= 1024; |
|
219 case 'k': |
|
220 $max *= 1024; |
|
221 } |
|
222 |
|
223 return (integer) $max; |
|
224 } |
|
225 } |