5use Psr\Http\Message\UploadedFileInterface;
7use Wikimedia\AtEase\AtEase;
49 switch ( $this->data[
'error'] ) {
53 case UPLOAD_ERR_INI_SIZE:
54 throw new RuntimeException(
'Upload exceeded maximum size' );
56 case UPLOAD_ERR_FORM_SIZE:
57 throw new RuntimeException(
'Upload exceeded form-specified maximum size' );
59 case UPLOAD_ERR_PARTIAL:
60 throw new RuntimeException(
'File was only partially uploaded' );
62 case UPLOAD_ERR_NO_FILE:
63 throw new RuntimeException(
'No file was uploaded' );
65 case UPLOAD_ERR_NO_TMP_DIR:
66 throw new RuntimeException(
'PHP has no temporary folder for storing uploaded files' );
68 case UPLOAD_ERR_CANT_WRITE:
69 throw new RuntimeException(
'PHP was unable to save the uploaded file' );
71 case UPLOAD_ERR_EXTENSION:
72 throw new RuntimeException(
'A PHP extension stopped the file upload' );
75 throw new RuntimeException(
'Unknown upload error code ' . $this->data[
'error'] );
79 throw new RuntimeException(
'File has already been moved' );
81 if ( !isset( $this->data[
'tmp_name'] ) || !file_exists( $this->data[
'tmp_name'] ) ) {
82 throw new RuntimeException(
'Uploaded file is missing' );
87 if ( $this->stream ) {
96 public function moveTo( $targetPath ) {
99 if ( $this->fromUpload && !is_uploaded_file( $this->data[
'tmp_name'] ) ) {
100 throw new RuntimeException(
'Specified file is not an uploaded file' );
104 if ( function_exists(
'error_clear_last' ) ) {
107 $ret = AtEase::quietCall(
108 $this->fromUpload ?
'move_uploaded_file' :
'rename',
109 $this->data[
'tmp_name'],
112 if ( $ret ===
false ) {
113 $err = error_get_last();
114 throw new RuntimeException(
"Move failed: " . ( $err[
'message'] ??
'Unknown error' ) );
118 if ( $this->stream ) {
119 $this->stream->close();
120 $this->stream =
null;
125 return $this->data[
'size'] ??
null;
129 return $this->data[
'error'] ?? UPLOAD_ERR_NO_FILE;
133 $ret = $this->data[
'name'] ??
null;
134 return $ret ===
'' ? null : $ret;
138 $ret = $this->data[
'type'] ??
null;
139 return $ret ===
'' ? null : $ret;