1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Psr7. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Http\Message; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Psr\Http\Message\StreamInterface; |
16
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
class UploadedFile implements UploadedFileInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Retrieve a stream representing the uploaded file. |
23
|
|
|
* |
24
|
|
|
* This method MUST return a StreamInterface instance, representing the |
25
|
|
|
* uploaded file. The purpose of this method is to allow utilizing native PHP |
26
|
|
|
* stream functionality to manipulate the file upload, such as |
27
|
|
|
* stream_copy_to_stream() (though the result will need to be decorated in a |
28
|
|
|
* native PHP stream wrapper to work with such functions). |
29
|
|
|
* |
30
|
|
|
* If the moveTo() method has been called previously, this method MUST raise |
31
|
|
|
* an exception. |
32
|
|
|
* |
33
|
|
|
* @return StreamInterface Stream representation of the uploaded file. |
34
|
|
|
* |
35
|
|
|
* @throws RuntimeException in cases when no stream is available or can becreated. |
36
|
|
|
*/ |
37
|
|
|
public function getStream(): StreamInterface |
38
|
|
|
{ |
39
|
|
|
} |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Move the uploaded file to a new location. |
43
|
|
|
* |
44
|
|
|
* Use this method as an alternative to move_uploaded_file(). This method is |
45
|
|
|
* guaranteed to work in both SAPI and non-SAPI environments. |
46
|
|
|
* Implementations must determine which environment they are in, and use the |
47
|
|
|
* appropriate method (move_uploaded_file(), rename(), or a stream |
48
|
|
|
* operation) to perform the operation. |
49
|
|
|
* |
50
|
|
|
* $targetPath may be an absolute path, or a relative path. If it is a |
51
|
|
|
* relative path, resolution should be the same as used by PHP's rename() |
52
|
|
|
* function. |
53
|
|
|
* |
54
|
|
|
* The original file or stream MUST be removed on completion. |
55
|
|
|
* |
56
|
|
|
* If this method is called more than once, any subsequent calls MUST raise |
57
|
|
|
* an exception. |
58
|
|
|
* |
59
|
|
|
* When used in an SAPI environment where $_FILES is populated, when writing |
60
|
|
|
* files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be |
61
|
|
|
* used to ensure permissions and upload status are verified correctly. |
62
|
|
|
* |
63
|
|
|
* If you wish to move to a stream, use getStream(), as SAPI operations |
64
|
|
|
* cannot guarantee writing to stream destinations. |
65
|
|
|
* |
66
|
|
|
* @see http://php.net/is_uploaded_file |
67
|
|
|
* @see http://php.net/move_uploaded_file |
68
|
|
|
* |
69
|
|
|
* @param string $targetPath Path to which to move the uploaded file. |
70
|
|
|
* |
71
|
|
|
* @throws InvalidArgumentException if the $targetPath specified is invalid. |
72
|
|
|
* @throws RuntimeException on any error during the move operation, or on |
73
|
|
|
* the second or subsequent call to the method. |
74
|
|
|
*/ |
75
|
|
|
public function moveTo(string $targetPath) |
76
|
|
|
{ |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retrieve the file size. |
81
|
|
|
* |
82
|
|
|
* Implementations SHOULD return the value stored in the "size" key of |
83
|
|
|
* the file in the $_FILES array if available, as PHP calculates this based |
84
|
|
|
* on the actual size transmitted. |
85
|
|
|
* |
86
|
|
|
* @return int The file size in bytes or zero if unknown. |
87
|
|
|
*/ |
88
|
|
|
public function getSize(): int |
89
|
|
|
{ |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Retrieve the error associated with the uploaded file. |
94
|
|
|
* |
95
|
|
|
* The return value MUST be one of PHP's UPLOAD_ERR_XXX constants. |
96
|
|
|
* |
97
|
|
|
* If the file was uploaded successfully, this method MUST return |
98
|
|
|
* UPLOAD_ERR_OK. |
99
|
|
|
* |
100
|
|
|
* Implementations SHOULD return the value stored in the "error" key of |
101
|
|
|
* the file in the $_FILES array. |
102
|
|
|
* |
103
|
|
|
* @see http://php.net/manual/en/features.file-upload.errors.php |
104
|
|
|
* |
105
|
|
|
* @return int One of PHP's UPLOAD_ERR_XXX constants. |
106
|
|
|
*/ |
107
|
|
|
public function getError(): int |
108
|
|
|
{ |
109
|
|
|
} |
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieve the filename sent by the client. |
113
|
|
|
* |
114
|
|
|
* Do not trust the value returned by this method. A client could send |
115
|
|
|
* a malicious filename with the intention to corrupt or hack your |
116
|
|
|
* application. |
117
|
|
|
* |
118
|
|
|
* Implementations SHOULD return the value stored in the "name" key of |
119
|
|
|
* the file in the $_FILES array. |
120
|
|
|
* |
121
|
|
|
* @return string The filename sent by the client or void string if none |
122
|
|
|
* was provided. |
123
|
|
|
*/ |
124
|
|
|
public function getClientFilename(): string |
125
|
|
|
{ |
126
|
|
|
} |
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieve the media type sent by the client. |
130
|
|
|
* |
131
|
|
|
* Do not trust the value returned by this method. A client could send |
132
|
|
|
* a malicious media type with the intention to corrupt or hack your |
133
|
|
|
* application. |
134
|
|
|
* |
135
|
|
|
* Implementations SHOULD return the value stored in the "type" key of |
136
|
|
|
* the file in the $_FILES array. |
137
|
|
|
* |
138
|
|
|
* @return string The media type sent by the client or void string if none |
139
|
|
|
* was provided. |
140
|
|
|
*/ |
141
|
|
|
public function getClientMediaType(): string |
142
|
|
|
{ |
143
|
|
|
} |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: