导航菜单
首页 >  How to Upload Files Symfony Docs  > File (Symfony Docs)

File (Symfony Docs)

OptionsbinaryFormat

type: boolean default: null

When true, the sizes will be displayed in messages with binary-prefixedunits (KiB, MiB). When false, the sizes will be displayed with SI-prefixedunits (kB, MB). When null, then the binaryFormat will be guessed fromthe value defined in the maxSize option.

For more information about the difference between binary and SI prefixes,see Wikipedia: Binary prefix.

extensions

type: array or string

If set, the validator will check that the extension and the media type(formerly known as MIME type) of the underlying file are equal to the givenextension and associated media type (if a string) or exist in the collection(if an array).

By default, all media types associated with an extension are allowed.The list of supported extensions and associated media types can be found onthe IANA website.

It's also possible to explicitly configure the authorized media types foran extension.

In the following example, allowed media types are explicitly set for the xmland txt extensions, and all associated media types are allowed for jpg:

12345['xml' => ['text/xml', 'application/xml'],'txt' => 'text/plain','jpg',]disallowEmptyMessage

type: string default: An empty file is not allowed.

This constraint checks if the uploaded file is empty (i.e. 0 bytes). If it is,this message is displayed.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file path{{ name }}Base file namegroups

type: array | string default: null

It defines the validation group or groups of this constraint. Read moreabout validation groups.

maxSize

type: mixed

If set, the size of the underlying file must be below this file size inorder to be valid. The size of the file can be given in one of the followingformats:

SuffixUnit NameValueExample(none)byte1 byte4096kkilobyte1,000 bytes200kMmegabyte1,000,000 bytes2MKikibibyte1,024 bytes32KiMimebibyte1,048,576 bytes8Mi

For more information about the difference between binary and SI prefixes,see Wikipedia: Binary prefix.

maxSizeMessage

type: string default: The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.

The message displayed if the file is larger than the maxSize option.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file path{{ limit }}Maximum file size allowed{{ name }}Base file name{{ size }}File size of the given file{{ suffix }}Suffix for the used file size unit (see above)mimeTypes

type: array or string

Caution

You should always use the extensions option instead of mimeTypesexcept if you explicitly don't want to check that the extension of the fileis consistent with its content (this can be a security issue).

By default, the extensions option also checks the media type of the file.

If set, the validator will check that the media type (formerly known as MIMEtype) of the underlying file is equal to the given mime type (if a string) orexists in the collection of given mime types (if an array).

You can find a list of existing mime types on the IANA website.

Note

When using this constraint on a FileType field,the value of the mimeTypes option is also used in the acceptattribute of the related HTML element.

This behavior is applied only when using form type guessing(i.e. the form type is not defined explicitly in the ->add() method ofthe form builder) and when the field doesn't define its own accept value.

filenameMaxLength

type: integer default: null

If set, the validator will check that the filename of the underlying filedoesn't exceed a certain length.

filenameTooLongMessage

type: string default: The filename is too long. It should have {{ filename_max_length }} character or less.|The filename is too long. It should have {{ filename_max_length }} characters or less.

The message displayed if the filename of the file exceeds the limit setwith the filenameMaxLength option.

You can use the following parameters in this message:

ParameterDescription{{ filename_max_length }}Maximum number of characters allowedextensionsMessage

type: string default: The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}.

The message displayed if the extension of the file is not a valid extensionper the extensions option.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file path{{ name }}Base file name{{ type }}The MIME type of the given file{{ types }}The list of allowed MIME typesmimeTypesMessage

type: string default: The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.

The message displayed if the media type of the file is not a valid media typeper the mimeTypes option.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file path{{ name }}Base file name{{ type }}The MIME type of the given file{{ types }}The list of allowed MIME typesnotFoundMessage

type: string default: The file could not be found.

The message displayed if no file can be found at the given path. This erroris only likely if the underlying value is a string path, as a File objectcannot be constructed with an invalid file path.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file pathnotReadableMessage

type: string default: The file is not readable.

The message displayed if the file exists, but the PHP is_readable() functionfails when passed the path to the file.

You can use the following parameters in this message:

ParameterDescription{{ file }}Absolute file pathpayload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint.The configured payload is not used by the Validator component, but its processingis completely up to you.

For example, you may want to useseveral error levels to present failedconstraints differently in the front-end depending on the severity of theerror.

uploadCantWriteErrorMessage

type: string default: Cannot write temporary file to disk.

The message that is displayed if the uploaded file can't be stored in thetemporary folder.

This message has no parameters.

uploadErrorMessage

type: string default: The file could not be uploaded.

The message that is displayed if the uploaded file could not be uploadedfor some unknown reason.

This message has no parameters.

uploadExtensionErrorMessage

type: string default: A PHP extension caused the upload to fail.

The message that is displayed if a PHP extension caused the file upload tofail.

This message has no parameters.

uploadFormSizeErrorMessage

type: string default: The file is too large.

The message that is displayed if the uploaded file is larger than allowedby the HTML file input field.

This message has no parameters.

uploadIniSizeErrorMessage

type: string default: The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.

The message that is displayed if the uploaded file is larger than the upload_max_filesizephp.ini setting.

You can use the following parameters in this message:

ParameterDescription{{ limit }}Maximum file size allowed{{ suffix }}Suffix for the used file size unit (see above)uploadNoFileErrorMessage

type: string default: No file was uploaded.

The message that is displayed if no file was uploaded.

This message has no parameters.

uploadNoTmpDirErrorMessage

type: string default: No temporary folder was configured in php.ini.

The message that is displayed if the php.ini setting upload_tmp_dir ismissing.

This message has no parameters.

uploadPartialErrorMessage

type: string default: The file was only partially uploaded.

The message that is displayed if the uploaded file is only partially uploaded.

This message has no parameters.

相关推荐: