-
Notifications
You must be signed in to change notification settings - Fork 2
PSR 7: UploadedFile Example
Terry L edited this page Jun 20, 2020
·
3 revisions
Namespace
Shieldon\Psr7\UploadedFile
-
param
string|StreamInterface
source*
The full path of a file or stream. -
param
string|null
name= null
The file name. -
param
string|null
type= null
The file media type. -
param
int|null
size= null
The file size in bytes. -
param
int
error= 0
The status code of the upload. -
param
string|null
sapi= null
Only 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
string
targetPath*
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: 100000
Retrieve 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: 0
Retrieve 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.jpg
Retrieve 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/jpeg
composer require shieldon/psr-http
Shieldon PSR HTTP implementation written by Terry L. from Taiwan.