# Class HttpSoft\UploadedFile\UploadedFileFactory

Factory for creating HttpSoft\UploadedFile\UploadedFile, implements Psr\Http\Message\UploadedFileFactoryInterface.

Source code on GitHub.

use HttpSoft\Stream\Stream;
use HttpSoft\UploadedFile\UploadedFileFactory;

$factory = new UploadedFileFactory();
$stream = new Stream('/tmp/phpN3FmFr', 'wb+');
$uploadedFile = $factory->createUploadedFile($stream, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
// equivalently to:
$uploadedFile = UploadedFileFactory::create($stream, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');

// From existing file:
$uploadedFile = UploadedFileFactory::create('/tmp/phpN3FmFr', 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
// From existing resource:
$resource = fopen('path/to/file', 'wb+');
$uploadedFile = UploadedFileFactory::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,
]);

$uploadedFile->getClientFilename(); // 'file.txt'
$uploadedFile->getClientMediaType(); // 'text/plain'
$uploadedFile->getStream()->getMetadata('uri'); // '/tmp/phpN3FmFr'
$uploadedFile->getError(); // 0
$uploadedFile->getSize(); // 1024

# Public methods

See the original detailed description of the methods in the Psr\Http\Message\UploadedFileFactoryInterface.

# create

Create a new HttpSoft\UploadedFile\UploadedFile instance (static method).

/**
 * @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

Create a new HttpSoft\UploadedFile\UploadedFile instance from array (static method).

public static function createFromArray(array $file): UploadedFileInterface;

Creates an instance of Psr\Http\Message\UploadedFileInterface from an 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,
];

For more information about the structure of uploaded files, see here.

# createUploadedFile

Create a new HttpSoft\UploadedFile\UploadedFile instance.

/**
 * @param StreamInterface $stream
 * @param int|null $size
 * @param int $error
 * @param string|null $clientFilename
 * @param string|null $clientMediaType
 * @return UploadedFileInterface
 */
public function createUploadedFile(
    StreamInterface $stream,
    int $size = null,
    int $error = UPLOAD_ERR_OK,
    string $clientFilename = null,
    string $clientMediaType = null
): UploadedFileInterface;