# Class HttpSoft\ServerRequest\UploadedFileCreator
Helper for creating HttpSoft\Message\UploadedFile.
use HttpSoft\Message\Stream;
use HttpSoft\ServerRequest\UploadedFileCreator;
// From existing file:
$uploadedFile = UploadedFileCreator::create('/tmp/phpN3FmFr', 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
// From `Psr\Http\Message\StreamInstance` instance:
$stream = new Stream('/tmp/phpN3FmFr', 'wb+');
$uploadedFile = UploadedFileCreator::create($stream, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
// From existing resource:
$resource = fopen('path/to/file', 'wb+');
$uploadedFile = UploadedFileCreator::create($resource, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
// From the item `$_FILES`:
$uploadedFile = UploadedFileFactory::createFromArray([
'name' => 'file.txt',
'type' => 'text/plain',
'tmp_name' => '/tmp/phpN3FmFr',
'error' => UPLOAD_ERR_OK,
'size' => 1024,
]);
// Create multiple from `$_FILES` superglobal array:
$uploadedFiles = UploadedFileCreator::createFromGlobals($_FILES);
# Public methods
# create
Creates a new HttpSoft\Message\UploadedFile instance.
/**
* @param StreamInterface|string|resource $streamOrFile
* @param int $size
* @param int $error
* @param string|null $clientFilename
* @param string|null $clientMediaType
* @return UploadedFileInterface
*/
public static function create(
$streamOrFile,
int $size,
int $error,
string $clientFilename = null,
string $clientMediaType = null
): UploadedFileInterface;
# createFromArray
Creates a new HttpSoft\Message\UploadedFile instance from array.
public static function createFromArray(array $file): UploadedFileInterface;
Creates an instance of Psr\Http\Message\UploadedFileInterface
from a one-dimensional array $file
. The array structure must be the same as item in the global $_FILES
array.
// Example of array structure:
[
'name' => 'filename.jpg', // optional
'type' => 'image/jpeg', // optional
'tmp_name' => '/tmp/php/php6hst32',
'error' => 0, // UPLOAD_ERR_OK
'size' => 98174,
];
# createFromGlobals
Normalizes the tree structure of downloaded files and converts each value of the multidimensional array $files
to an Psr\Http\Message\UploadedFileInterface instance.
public static function createFromGlobals(array $files = []): array;
The method uses recursion, so the $files
array can be of any nesting type.
All key names in the $files
array will be saved.
The array structure must be the same as the global
$_FILES
array.
If specified invalid value file specification, the \InvalidArgumentException
exception will be thrown.
use HttpSoft\ServerRequest\UploadedFileCreator;
// Example #1
print_r($_FILES);
/*
[
'file' => [
'name' => 'file.txt',
'type' => 'text/plain',
'tmp_name' => '/tmp/phpN3FmFr',
'error' => UPLOAD_ERR_OK,
'size' => 1024,
],
];
*/
print_r(UploadedFileCreator::createFromGlobals($_FILES));
/*
[
'file' => `HttpSoft\Message\UploadedFile Object`,
];
*/
// Example #2
/*
[
'file_1' => [
'name' => 'file.txt',
'type' => 'text/plain',
'tmp_name' => '/tmp/phpN3FmFr',
'error' => UPLOAD_ERR_OK,
'size' => 1024,
],
'file_2' => [
'name' => 'image.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpLs7DaJ',
'error' => UPLOAD_ERR_OK,
'size' => 98760,
],
];
*/
print_r(UploadedFileCreator::createFromGlobals($_FILES));
/*
[
'file_1' => `HttpSoft\Message\UploadedFile Object`,
'file_2' => `HttpSoft\Message\UploadedFile Object`,
];
*/
// Example #3
print_r($_FILES);
/*
[
'files' => [
'name' => [
'file_1' => 'file.txt',
'file_2' => 'image.png',
],
'type' => [
'file_1' => 'text/plain',
'file_2' => 'image/png',
],
'tmp_name' => [
'file_1' => '/tmp/phpN3FmFr',
'file_2' => '/tmp/phpLs7DaJ',
],
'error' => [
'file_1' => 0,
'file_2' => 0,
],
'size' => [
'file_1' => 1024,
'file_2' => 98174,
],
],
];
*/
print_r(UploadedFileCreator::createFromGlobals($_FILES));
/*
[
'files' => [
'file_1' => `HttpSoft\Message\UploadedFile Object`,
'file_2' => `HttpSoft\Message\UploadedFile Object`,
],
];
*/
// Example #4
/*
[
'files' => [
'name' => [
'data' => [
'nested' => [
'file_1' => 'file.txt',
'file_2' => 'image.png',
],
],
],
'type' => [
'data' => [
'nested' => [
'file_1' => 'text/plain',
'file_2' => 'image/png',
],
],
],
'tmp_name' => [
'data' => [
'nested' => [
'file_1' => '/tmp/phpN3FmFr',
'file_2' => '/tmp/phpLs7DaJ',
],
],
],
'error' => [
'data' => [
'nested' => [
'file_1' => UPLOAD_ERR_OK,
'file_2' => UPLOAD_ERR_OK,
],
],
],
'size' => [
'data' => [
'nested' => [
'file_1' => 1024,
'file_2' => 98174,
],
],
],
],
];
*/
print_r(UploadedFileCreator::createFromGlobals($_FILES));
/*
[
'files' => [
'data' => [
'nested' => [
'file_1' => `HttpSoft\Message\UploadedFile Object`,
'file_2' => `HttpSoft\Message\UploadedFile Object`,
],
],
],
];
*/
For more information about the structure of uploaded files, see here.