5use InvalidArgumentException;
6use Psr\Http\Message\UploadedFileInterface;
7use UnexpectedValueException;
37 public function getValue( $name, array $settings, array $options ) {
38 $ret = $this->callbacks->getUploadedFile( $name, $options );
40 if ( $ret && $ret->getError() === UPLOAD_ERR_NO_FILE &&
41 !$this->callbacks->hasParam( $name, $options )
46 } elseif ( !$ret && $this->callbacks->hasParam( $name, $options ) ) {
71 return ini_get(
'upload_max_filesize' );
75 public function validate( $name, $value, array $settings, array $options ) {
78 UPLOAD_ERR_FORM_SIZE =>
'formsize',
79 UPLOAD_ERR_PARTIAL =>
'partial',
80 UPLOAD_ERR_NO_FILE =>
'nofile',
81 UPLOAD_ERR_NO_TMP_DIR =>
'notmpdir',
82 UPLOAD_ERR_CANT_WRITE =>
'cantwrite',
83 UPLOAD_ERR_EXTENSION =>
'phpext',
86 if ( !$value instanceof UploadedFileInterface ) {
88 $type = get_debug_type( $value );
89 throw new InvalidArgumentException(
"\$value must be UploadedFileInterface, got $type" );
92 $err = $value->getError();
93 if ( $err === UPLOAD_ERR_OK ) {
95 } elseif ( $err === UPLOAD_ERR_INI_SIZE ) {
102 $last = strtolower( substr( $size, -1 ) );
103 $size = intval( $size, 10 ) * ( $prefixes[$last] ?? 1 );
108 ],
'inisize' )->sizeParams( $size ),
109 $name,
'', $settings, $options
111 } elseif ( isset( $codemap[$err] ) ) {
113 $this->
failureMessage(
'badupload', [
'code' => $codemap[$err] ], $codemap[$err] ),
114 $name,
'', $settings, $options
118 foreach ( get_defined_constants() as $c => $v ) {
120 if ( $v === $err && str_starts_with( $c,
'UPLOAD_ERR_' ) ) {
121 $constant =
" ($c?)";
124 throw new UnexpectedValueException(
"Unrecognized PHP upload error value $err$constant" );
129 public function checkSettings(
string $name, $settings, array $options, array $ret ): array {
130 $ret = parent::
checkSettings( $name, $settings, $options, $ret );
134 'Cannot specify a default for upload-type parameters';
141 'PARAM_ISMULTI cannot be used for upload-type parameters';
148 public function stringifyValue( $name, $value, array $settings, array $options ) {
154 public function getHelpInfo( $name, array $settings, array $options ) {
155 $info = parent::getHelpInfo( $name, $settings, $options );