函数文档

wp_max_upload_size()

💡 云策文档标注

概述

wp_max_upload_size() 函数用于确定 PHP 配置中允许的最大上传文件大小,返回以字节为单位的整数值。它通过比较 upload_max_filesize 和 post_max_size 设置,并应用 upload_size_limit 过滤器来计算结果。

关键要点

  • 函数返回 PHP 配置中允许的最大上传文件大小,单位为字节。
  • 通过 wp_convert_hr_to_bytes() 转换 ini_get('upload_max_filesize') 和 ini_get('post_max_size') 为字节值。
  • 使用 min() 函数取 upload_max_filesize 和 post_max_size 中的较小值作为基础限制。
  • 应用 upload_size_limit 过滤器,允许开发者通过 apply_filters() 修改最大上传大小。
  • 函数自 WordPress 2.5.0 版本引入,无参数,直接调用即可。

代码示例

$max_upload_size = wp_max_upload_size(); // 返回整数字节值

📄 原文内容

Determines the maximum upload size allowed in php.ini.

Return

int Allowed upload size.

Source

function wp_max_upload_size() {
	$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
	$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );

	/**
	 * Filters the maximum upload size allowed in php.ini.
	 *
	 * @since 2.5.0
	 *
	 * @param int $size    Max upload size limit in bytes.
	 * @param int $u_bytes Maximum upload filesize in bytes.
	 * @param int $p_bytes Maximum size of POST data in bytes.
	 */
	return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
}

Hooks

apply_filters( ‘upload_size_limit’, int $size, int $u_bytes, int $p_bytes )

Filters the maximum upload size allowed in php.ini.

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes