upload_is_file_too_big()
云策文档标注
概述
upload_is_file_too_big() 函数用于检查上传文件是否超过大小限制。它基于文件数据和站点设置进行验证,返回文件数组或错误消息。
关键要点
- 函数接受一个数组参数 $upload,包含新上传文件的信息。
- 如果上传文件大小未超过限制,返回 $upload 数组;否则返回错误消息字符串。
- 函数内部检查 WP_IMPORTING 常量和 upload_space_check_disabled 选项,以跳过验证。
- 使用 get_site_option('fileupload_maxk', 1500) 获取最大文件大小(默认 1500 KB),并与文件数据比较。
- 错误消息通过 __() 函数进行本地化处理。
代码示例
function upload_is_file_too_big( $upload ) {
if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
return $upload;
}
if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
/* translators: %s: Maximum allowed file size in kilobytes. */
return sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ) . '', get_site_option( 'fileupload_maxk', 1500 ) );
}
return $upload;
}注意事项
- 函数在 WordPress MU 3.0.0 版本中引入。
- 相关函数包括 __() 用于翻译和 get_site_option() 用于获取网络选项。
- 文件大小限制基于 KB_IN_BYTES 常量计算,确保单位一致。
原文内容
Checks whether an upload is too big.
Parameters
$uploadarrayrequired-
An array of information about the newly-uploaded file.
Source
function upload_is_file_too_big( $upload ) {
if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
return $upload;
}
if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
/* translators: %s: Maximum allowed file size in kilobytes. */
return sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
}
return $upload;
}
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |