-
Couldn't load subscription status.
- Fork 1
PSR 7: UploadedFile Example
Terry L edited this page Jun 20, 2020
·
3 revisions
Namespace
Shieldon\Psr7\UploadedFile-
param
string|StreamInterfacesource*The full path of a file or stream. -
param
string|nullname= nullThe file name. -
param
string|nulltype= nullThe file media type. -
param
int|nullsize= nullThe file size in bytes. -
param
interror= 0The status code of the upload. -
param
string|nullsapi= nullOnly assign for unit testing purpose.
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp', // source
'example1.jpg', // name
'image/jpeg', // type
100000, // size
0 // error
);Retrieve a stream representing the uploaded file.
-
return
StreamInterface
Example:
$stream = new Stream(fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+'));
$uploadedFile = new UploadedFile($stream);
$stream2 = $uploadedFile->getStream();
echo $stream2->getMetadata('mode');
// Outputs: r+Move the uploaded file to a new location.
-
param
stringtargetPath*Path to which to move the uploaded file.
$stream = new Stream(
fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+')
);
$uploadedFile = new UploadedFile($stream);
$uploadedFile->moveTo('/home/terrylin/public/image_cache/shieldon_logo_png');
if (
file_exists('/home/terrylin/public/image_cache/shieldon_logo_png') &&
! file_exists(BOOTSTRAP_DIR . '/sample/shieldon_logo.png')
) {
echo 'File has been moved to the new place.';
} else {
echo 'Cannot move file.';
}
// Outputs: File has been moved to the new place.Retrieve the file size.
-
return
int|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
echo $uploadedFile->getSize();
// Outputs: 100000Retrieve the error associated with the uploaded file.
-
return
int
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getError();
// Outputs: 0Retrieve the filename sent by the client.
-
return
string|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getClientFilename();
// Outputs: example1.jpgRetrieve the media type sent by the client.
-
return
string|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getClientMediaType();
// Outputs: image/jpegcomposer require shieldon/psr-httpShieldon PSR HTTP implementation written by Terry L. from Taiwan.
