ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
UploadedFile.php
Go to the documentation of this file.
1<?php
9namespace Slim\Http;
10
11use RuntimeException;
12use InvalidArgumentException;
15
25{
33 public $file;
39 protected $name;
45 protected $type;
51 protected $size;
57 protected $error = UPLOAD_ERR_OK;
63 protected $sapi = false;
69 protected $stream;
75 protected $moved = false;
76
84 public static function createFromEnvironment(Environment $env)
85 {
86 if (is_array($env['slim.files']) && $env->has('slim.files')) {
87 return $env['slim.files'];
88 } elseif (isset($_FILES)) {
89 return static::parseUploadedFiles($_FILES);
90 }
91
92 return [];
93 }
94
102 private static function parseUploadedFiles(array $uploadedFiles)
103 {
104 $parsed = [];
105 foreach ($uploadedFiles as $field => $uploadedFile) {
106 if (!isset($uploadedFile['error'])) {
107 if (is_array($uploadedFile)) {
108 $parsed[$field] = static::parseUploadedFiles($uploadedFile);
109 }
110 continue;
111 }
112
113 $parsed[$field] = [];
114 if (!is_array($uploadedFile['error'])) {
115 $parsed[$field] = new static(
116 $uploadedFile['tmp_name'],
117 isset($uploadedFile['name']) ? $uploadedFile['name'] : null,
118 isset($uploadedFile['type']) ? $uploadedFile['type'] : null,
119 isset($uploadedFile['size']) ? $uploadedFile['size'] : null,
120 $uploadedFile['error'],
121 true
122 );
123 } else {
124 $subArray = [];
125 foreach ($uploadedFile['error'] as $fileIdx => $error) {
126 // normalise subarray and re-parse to move the input's keyname up a level
127 $subArray[$fileIdx]['name'] = $uploadedFile['name'][$fileIdx];
128 $subArray[$fileIdx]['type'] = $uploadedFile['type'][$fileIdx];
129 $subArray[$fileIdx]['tmp_name'] = $uploadedFile['tmp_name'][$fileIdx];
130 $subArray[$fileIdx]['error'] = $uploadedFile['error'][$fileIdx];
131 $subArray[$fileIdx]['size'] = $uploadedFile['size'][$fileIdx];
132
133 $parsed[$field] = static::parseUploadedFiles($subArray);
134 }
135 }
136 }
137
138 return $parsed;
139 }
140
151 public function __construct($file, $name = null, $type = null, $size = null, $error = UPLOAD_ERR_OK, $sapi = false)
152 {
153 $this->file = $file;
154 $this->name = $name;
155 $this->type = $type;
156 $this->size = $size;
157 $this->error = $error;
158 $this->sapi = $sapi;
159 }
160
177 public function getStream()
178 {
179 if ($this->moved) {
180 throw new \RuntimeException(sprintf('Uploaded file %s has already been moved', $this->name));
181 }
182 if ($this->stream === null) {
183 $this->stream = new Stream(fopen($this->file, 'r'));
184 }
185
186 return $this->stream;
187 }
188
223 public function moveTo($targetPath)
224 {
225 if ($this->moved) {
226 throw new RuntimeException('Uploaded file already moved');
227 }
228
229 $targetIsStream = strpos($targetPath, '://') > 0;
230 if (!$targetIsStream && !is_writable(dirname($targetPath))) {
231 throw new InvalidArgumentException('Upload target path is not writable');
232 }
233
234 if ($targetIsStream) {
235 if (!copy($this->file, $targetPath)) {
236 throw new RuntimeException(sprintf('Error moving uploaded file %s to %s', $this->name, $targetPath));
237 }
238 if (!unlink($this->file)) {
239 throw new RuntimeException(sprintf('Error removing uploaded file %s', $this->name));
240 }
241 } elseif ($this->sapi) {
242 if (!is_uploaded_file($this->file)) {
243 throw new RuntimeException(sprintf('%s is not a valid uploaded file', $this->file));
244 }
245
246 if (!move_uploaded_file($this->file, $targetPath)) {
247 throw new RuntimeException(sprintf('Error moving uploaded file %s to %s', $this->name, $targetPath));
248 }
249 } else {
250 if (!rename($this->file, $targetPath)) {
251 throw new RuntimeException(sprintf('Error moving uploaded file %s to %s', $this->name, $targetPath));
252 }
253 }
254
255 $this->moved = true;
256 }
257
273 public function getError()
274 {
275 return $this->error;
276 }
277
291 public function getClientFilename()
292 {
293 return $this->name;
294 }
295
309 public function getClientMediaType()
310 {
311 return $this->type;
312 }
313
323 public function getSize()
324 {
325 return $this->size;
326 }
327}
sprintf('%.4f', $callTime)
$env
An exception for terminatinating execution or to throw for unit testing.
Represents a data stream as defined in PSR-7.
Definition: Stream.php:21
Represents Uploaded Files.
getError()
Retrieve the error associated with the uploaded file.
static parseUploadedFiles(array $uploadedFiles)
Parse a non-normalized, i.e.
static createFromEnvironment(Environment $env)
Create a normalized tree of UploadedFile instances from the Environment.
getSize()
Retrieve the file size.
moveTo($targetPath)
Move the uploaded file to a new location.
getClientMediaType()
Retrieve the media type sent by the client.
__construct($file, $name=null, $type=null, $size=null, $error=UPLOAD_ERR_OK, $sapi=false)
Construct a new UploadedFile instance.
getStream()
Retrieve a stream representing the uploaded file.
getClientFilename()
Retrieve the filename sent by the client.
error($a_errmsg)
set error message @access public
Describes a data stream.
Value object representing a file uploaded through an HTTP request.
Slim Framework (https://slimframework.com)
Definition: Body.php:9
$uploadedFile
Definition: imgupload.php:61