ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Slim\Http\UploadedFile Class Reference

Represents Uploaded Files. More...

+ Inheritance diagram for Slim\Http\UploadedFile:
+ Collaboration diagram for Slim\Http\UploadedFile:

Public Member Functions

 __construct ($file, $name=null, $type=null, $size=null, $error=UPLOAD_ERR_OK, $sapi=false)
 Construct a new UploadedFile instance. More...
 
 getStream ()
 Retrieve a stream representing the uploaded file. More...
 
 moveTo ($targetPath)
 Move the uploaded file to a new location. More...
 
 getError ()
 Retrieve the error associated with the uploaded file. More...
 
 getClientFilename ()
 Retrieve the filename sent by the client. More...
 
 getClientMediaType ()
 Retrieve the media type sent by the client. More...
 
 getSize ()
 Retrieve the file size. More...
 
 getStream ()
 Retrieve a stream representing the uploaded file. More...
 
 moveTo ($targetPath)
 Move the uploaded file to a new location. More...
 
 getSize ()
 Retrieve the file size. More...
 
 getError ()
 Retrieve the error associated with the uploaded file. More...
 
 getClientFilename ()
 Retrieve the filename sent by the client. More...
 
 getClientMediaType ()
 Retrieve the media type sent by the client. More...
 

Static Public Member Functions

static createFromEnvironment (Environment $env)
 Create a normalized tree of UploadedFile instances from the Environment. More...
 

Data Fields

 $file
 

Protected Attributes

 $name
 
 $type
 
 $size
 
 $error = UPLOAD_ERR_OK
 
 $sapi = false
 
 $stream
 
 $moved = false
 

Static Private Member Functions

static parseUploadedFiles (array $uploadedFiles)
 Parse a non-normalized, i.e. More...
 

Detailed Description

Represents Uploaded Files.

It manages and normalizes uploaded files according to the PSR-7 standard.

https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php

Definition at line 24 of file UploadedFile.php.

Constructor & Destructor Documentation

◆ __construct()

Slim\Http\UploadedFile::__construct (   $file,
  $name = null,
  $type = null,
  $size = null,
  $error = UPLOAD_ERR_OK,
  $sapi = false 
)

Construct a new UploadedFile instance.

Parameters
string$fileThe full path to the uploaded file provided by the client.
string | null$nameThe file name.
string | null$typeThe file media type.
int | null$sizeThe file size in bytes.
int$errorThe UPLOAD_ERR_XXX code representing the status of the upload.
bool$sapiIndicates if the upload is in a SAPI environment.

Definition at line 151 of file UploadedFile.php.

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 }
error($a_errmsg)
set error message @access public

References Slim\Http\UploadedFile\$error, Slim\Http\UploadedFile\$file, Slim\Http\UploadedFile\$name, Slim\Http\UploadedFile\$sapi, Slim\Http\UploadedFile\$size, Slim\Http\UploadedFile\$type, and error().

+ Here is the call graph for this function:

Member Function Documentation

◆ createFromEnvironment()

static Slim\Http\UploadedFile::createFromEnvironment ( Environment  $env)
static

Create a normalized tree of UploadedFile instances from the Environment.

Parameters
Environment$envThe environment
Returns
array|null A normalized tree of UploadedFile instances or null if none are provided.

Definition at line 84 of file UploadedFile.php.

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 }
$env

References $env.

Referenced by Slim\Http\Request\createFromEnvironment().

+ Here is the caller graph for this function:

◆ getClientFilename()

Slim\Http\UploadedFile::getClientFilename ( )

Retrieve the filename sent by the client.

Do not trust the value returned by this method. A client could send a malicious filename with the intention to corrupt or hack your application.

Implementations SHOULD return the value stored in the "name" key of the file in the $_FILES array.

Returns
string|null The filename sent by the client or null if none was provided.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 291 of file UploadedFile.php.

292 {
293 return $this->name;
294 }

References Slim\Http\UploadedFile\$name.

◆ getClientMediaType()

Slim\Http\UploadedFile::getClientMediaType ( )

Retrieve the media type sent by the client.

Do not trust the value returned by this method. A client could send a malicious media type with the intention to corrupt or hack your application.

Implementations SHOULD return the value stored in the "type" key of the file in the $_FILES array.

Returns
string|null The media type sent by the client or null if none was provided.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 309 of file UploadedFile.php.

310 {
311 return $this->type;
312 }

References Slim\Http\UploadedFile\$type.

◆ getError()

Slim\Http\UploadedFile::getError ( )

Retrieve the error associated with the uploaded file.

The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.

If the file was uploaded successfully, this method MUST return UPLOAD_ERR_OK.

Implementations SHOULD return the value stored in the "error" key of the file in the $_FILES array.

See also
http://php.net/manual/en/features.file-upload.errors.php
Returns
int One of PHP's UPLOAD_ERR_XXX constants.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 273 of file UploadedFile.php.

274 {
275 return $this->error;
276 }

References Slim\Http\UploadedFile\$error.

◆ getSize()

Slim\Http\UploadedFile::getSize ( )

Retrieve the file size.

Implementations SHOULD return the value stored in the "size" key of the file in the $_FILES array if available, as PHP calculates this based on the actual size transmitted.

Returns
int|null The file size in bytes or null if unknown.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 323 of file UploadedFile.php.

324 {
325 return $this->size;
326 }

References Slim\Http\UploadedFile\$size.

◆ getStream()

Slim\Http\UploadedFile::getStream ( )

Retrieve a stream representing the uploaded file.

This method MUST return a StreamInterface instance, representing the uploaded file. The purpose of this method is to allow utilizing native PHP stream functionality to manipulate the file upload, such as stream_copy_to_stream() (though the result will need to be decorated in a native PHP stream wrapper to work with such functions).

If the moveTo() method has been called previously, this method MUST raise an exception.

Returns
StreamInterface Stream representation of the uploaded file.
Exceptions

RuntimeException in cases when no stream is available or can be created.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 177 of file UploadedFile.php.

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 }
sprintf('%.4f', $callTime)

References Slim\Http\UploadedFile\$stream, and sprintf.

◆ moveTo()

Slim\Http\UploadedFile::moveTo (   $targetPath)

Move the uploaded file to a new location.

Use this method as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments. Implementations must determine which environment they are in, and use the appropriate method (move_uploaded_file(), rename(), or a stream operation) to perform the operation.

$targetPath may be an absolute path, or a relative path. If it is a relative path, resolution should be the same as used by PHP's rename() function.

The original file or stream MUST be removed on completion.

If this method is called more than once, any subsequent calls MUST raise an exception.

When used in an SAPI environment where $_FILES is populated, when writing files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be used to ensure permissions and upload status are verified correctly.

If you wish to move to a stream, use getStream(), as SAPI operations cannot guarantee writing to stream destinations.

See also
http://php.net/is_uploaded_file
http://php.net/move_uploaded_file
Parameters
string$targetPathPath to which to move the uploaded file.
Exceptions
InvalidArgumentExceptionif the $path specified is invalid.
RuntimeExceptionon any error during the move operation, or on the second or subsequent call to the method.

Implements Psr\Http\Message\UploadedFileInterface.

Definition at line 223 of file UploadedFile.php.

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 }

References sprintf.

◆ parseUploadedFiles()

static Slim\Http\UploadedFile::parseUploadedFiles ( array  $uploadedFiles)
staticprivate

Parse a non-normalized, i.e.

$_FILES superglobal, tree of uploaded file data.

Parameters
array$uploadedFilesThe non-normalized tree of uploaded file data.
Returns
array A normalized tree of UploadedFile instances.

Definition at line 102 of file UploadedFile.php.

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 }
$uploadedFile
Definition: imgupload.php:61

References Slim\Http\UploadedFile\$error, and $uploadedFile.

Field Documentation

◆ $error

Slim\Http\UploadedFile::$error = UPLOAD_ERR_OK
protected

◆ $file

Slim\Http\UploadedFile::$file

Definition at line 33 of file UploadedFile.php.

Referenced by Slim\Http\UploadedFile\__construct().

◆ $moved

Slim\Http\UploadedFile::$moved = false
protected

Definition at line 75 of file UploadedFile.php.

◆ $name

Slim\Http\UploadedFile::$name
protected

◆ $sapi

Slim\Http\UploadedFile::$sapi = false
protected

Definition at line 63 of file UploadedFile.php.

Referenced by Slim\Http\UploadedFile\__construct().

◆ $size

Slim\Http\UploadedFile::$size
protected

◆ $stream

Slim\Http\UploadedFile::$stream
protected

Definition at line 69 of file UploadedFile.php.

Referenced by Slim\Http\UploadedFile\getStream().

◆ $type

Slim\Http\UploadedFile::$type
protected

The documentation for this class was generated from the following file: