|
| 1 | +# File |
| 2 | + |
| 3 | +`File` checks that a value is a file and can validate its extension, MIME type, and size. |
| 4 | + |
| 5 | +Supported values: |
| 6 | + |
| 7 | +- string file paths; |
| 8 | +- `SplFileInfo` instances; |
| 9 | +- [PSR-7] `UploadedFileInterface` instances. |
| 10 | + |
| 11 | +Use `Each` for multiple files: |
| 12 | + |
| 13 | +```php |
| 14 | +use Yiisoft\Validator\Rule\Each; |
| 15 | +use Yiisoft\Validator\Rule\File; |
| 16 | + |
| 17 | +$rules = [ |
| 18 | + 'attachments' => new Each(new File(extensions: ['pdf', 'txt'])), |
| 19 | +]; |
| 20 | +``` |
| 21 | + |
| 22 | +For example, with a PSR-7 request: |
| 23 | + |
| 24 | +```php |
| 25 | +use Yiisoft\Validator\Rule\Each; |
| 26 | +use Yiisoft\Validator\Rule\File; |
| 27 | +use Yiisoft\Validator\Validator; |
| 28 | + |
| 29 | +$uploadedFiles = $request->getUploadedFiles()['attachments'] ?? []; |
| 30 | + |
| 31 | +$result = (new Validator())->validate( |
| 32 | + ['attachments' => $uploadedFiles], |
| 33 | + ['attachments' => new Each(new File(maxSize: 5_000_000))], |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +If your application works with native PHP `$_FILES` data directly, convert it to supported values such as PSR-7 uploaded |
| 38 | +file objects before passing it to the validator. |
| 39 | + |
| 40 | +## Uploaded Files |
| 41 | + |
| 42 | +`File` handles PSR-7 upload error codes. `UPLOAD_ERR_NO_FILE` is treated as a missing value, so optional upload fields |
| 43 | +can use `skipOnEmpty`: |
| 44 | + |
| 45 | +```php |
| 46 | +use Yiisoft\Validator\Rule\File; |
| 47 | + |
| 48 | +$rule = new File(skipOnEmpty: true); |
| 49 | +``` |
| 50 | + |
| 51 | +Other upload error codes fail validation with `uploadFailedMessage`. |
| 52 | + |
| 53 | +The rule does not prove that arbitrary string paths or `SplFileInfo` values came from PHP's HTTP upload mechanism. Do |
| 54 | +not pass user-submitted paths directly. Use PSR-7 uploaded file objects from a trusted request implementation, or perform |
| 55 | +[upload provenance checks] before validating filesystem paths. |
| 56 | + |
| 57 | +## MIME Types |
| 58 | + |
| 59 | +For filesystem-backed files, MIME type validation uses PHP's file information facilities through |
| 60 | +`mime_content_type()`. If the MIME type can't be determined, MIME validation fails. |
| 61 | + |
| 62 | +For pathless PSR-7 uploads backed only by an in-memory stream, `File` doesn't trust client-provided media type by |
| 63 | +default. If your application has already decided that the client metadata is acceptable for this field, enable it |
| 64 | +explicitly: |
| 65 | + |
| 66 | +```php |
| 67 | +use Yiisoft\Validator\Rule\File; |
| 68 | + |
| 69 | +$rule = new File( |
| 70 | + mimeTypes: ['text/plain'], |
| 71 | + trustClientMediaType: true, |
| 72 | +); |
| 73 | +``` |
| 74 | + |
| 75 | +This option should be used with care because the client can send any media type value. |
| 76 | + |
| 77 | +## Size |
| 78 | + |
| 79 | +For filesystem-backed uploads, size checks use the actual file size on disk. For pathless streams, size checks use the |
| 80 | +PSR-7 upload size when available. If a size constraint is configured and the size can't be determined, validation fails. |
| 81 | + |
| 82 | +`size` is mutually exclusive with `minSize` and `maxSize`. When both `minSize` and `maxSize` are set, `minSize` must be |
| 83 | +less than or equal to `maxSize`. |
| 84 | + |
| 85 | +## Request Body Streams |
| 86 | + |
| 87 | +`File` doesn't validate generic request body streams such as data read from `php://input` for PUT requests. Convert such |
| 88 | +input to a supported value first, or write a custom rule that validates your stream format and storage flow. |
| 89 | + |
| 90 | +[PSR-7]: https://www.php-fig.org/psr/psr-7/ |
| 91 | +[upload provenance checks]: https://www.php.net/manual/en/features.file-upload.post-method.php |
0 commit comments