HTTP Uploaded File (PSR-7)
Phalcon\Http\Message\UploadedFile is an implementation of the HTTP messaging interface as defined by PHP-FIG.
The is a value object class that stores information for the uploaded files to your application. making it easier to work with. There are several limitations when using just the superglobal, which the Phalcon\Http\Message\UploadedFile resolves. The object allows you to retrieve all the uploaded files in a normalized structure, which each leaf is a Phalcon\Http\Message\UploadedFile object.
We are creating a new using the memory stream, with size 0
, specifying that there was no upload error (UOLOAD_ERR_OK
) and the name of the file is phalcon.txt
. This information is available to us when working with the Phalcon\Http\Message\ServerRequest object automatically.
public function __construct(
StreamInterface | string | null $stream
[, int $size = null
[, int error = 0
[, string clientFilename = null
[, string clientMediaType = null ]]]]
)
stream
- A valid stream for the file (StreamInterface
,string
)size
- The size of the fileerror
- An upload error (see theUPLOADERR*
PHP )clientFilename
- The name of the uploaded file from the client
Returns the filename sent by the client. You should not trust the value returned by this method. The client could very well send a malicious filename with the intent to corrupt or hack your application. The value returned is the value stored in the name
key in the $_FILES
array.
Returns the media type sent by the client. You should not trust the value returned by this method. The client could very well send a malicious filename with the intent to corrupt or hack your application. The value returned is the value stored in the type
key in the $_FILES
array.
<?php
use Phalcon\Http\Message\UploadedFile;
$file = new UploadedFile(
'php://memory',
0,
UPLOAD_ERR_OK,
'phalcon.txt',
'application/text'
);
echo $file->getClientMediaType(); // 'application/text'
Returns the error associated with the uploaded file. The value is PHP’s UPLOADERR*
constants. If the file was uploaded successfully, the method will return UPLOAD_ERR_OK
. The value returned is the value stored in the error
key in the $_FILES
array.
Returns the size of the uploaded file. The value returned is the value stored in the size
key in the $_FILES
array if available.
<?php
use Phalcon\Http\Message\UploadedFile;
$file = new UploadedFile(
'php://memory',
'phalcon.txt'
);
echo $file->getSize(); // 1234
If the moveTo()
method has been called previously, a exception will be thrown.
Moves the uploaded file to a new location. This method should be used as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments.
The parameter $targetPath
can be an absolute or relative path. When calling this method, the original file or stream will be removed. As noted above, if this method is called more than once, any subsequent calls will raise a exception.
The method performs necessary checks internally so that permissions are properly maintained. If you need to to move the file to a stream, you will need to use getStream()
, since SAPI operations cannot guarantee writing stream destinations.
<?php
use Phalcon\Http\Message\Stream;
use Phalcon\Http\Message\UploadedFile;
$fileName = dataFolder('/assets/stream/mit.txt');
$stream = new Stream($fileName, 'rb');
$file = new UploadedFile(
$stream,
1234,
UPLOAD_ERR_OK,
'phalcon.txt'
);
$file->moveTo('/storage/files/');