函数文档

check_upload_size()

💡 云策文档标注

概述

check_upload_size() 函数用于检查上传文件是否超出空间配额,是 WordPress 多站点环境下的文件上传验证工具。它基于站点设置和用户配额进行判断,并在文件过大或空间不足时设置错误信息。

关键要点

  • 函数接受一个 $_FILES 数组元素作为参数,返回修改后的数组,若文件超出配额则设置 'error' 键。
  • 检查过程包括:验证上传空间检查是否禁用、文件是否已有错误、是否处于导入模式、剩余空间是否足够以及用户是否超出配额。
  • 错误处理:如果检测到错误且非 HTML 上传或 Ajax 请求,会调用 wp_die() 终止执行并显示错误消息。

代码示例

function check_upload_size( $file ) {
	if ( get_site_option( 'upload_space_check_disabled' ) ) {
		return $file;
	}

	if ( $file['error'] > 0 ) { // There's already an error.
		return $file;
	}

	if ( defined( 'WP_IMPORTING' ) ) {
		return $file;
	}

	$space_left = get_upload_space_available();

	$file_size = filesize( $file['tmp_name'] );
	if ( $space_left < $file_size ) {
		$file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
	} elseif ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
		/* translators: %s: Maximum allowed file size in kilobytes. */
		$file['error'] = sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) );
	}

	if ( upload_is_user_over_quota( false ) ) {
		$file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
	}

	if ( $file['error'] > 0 && ! isset( $_POST['html-upload'] ) && ! wp_doing_ajax() ) {
		wp_die( $file['error'] . ' <a href="javascript:history.back()">' . __( 'Back' ) . '</a>' );
	}

	return $file;
}

注意事项

  • 函数依赖于多站点相关函数如 get_upload_space_available() 和 upload_is_user_over_quota(),仅适用于 WordPress 多站点环境。
  • 错误消息使用 __() 进行国际化处理,开发者可提供翻译文件以支持多语言。
  • 在 WP_IMPORTING 常量定义时(如导入操作),函数会跳过检查,避免干扰批量上传过程。

📄 原文内容

Determines whether uploaded file exceeds space quota.

Parameters

$filearrayrequired
An element from the $_FILES array for a given file.

Return

array The $_FILES array element with 'error' key set if file exceeds quota. 'error' is empty otherwise.

Source

function check_upload_size( $file ) {
	if ( get_site_option( 'upload_space_check_disabled' ) ) {
		return $file;
	}

	if ( $file['error'] > 0 ) { // There's already an error.
		return $file;
	}

	if ( defined( 'WP_IMPORTING' ) ) {
		return $file;
	}

	$space_left = get_upload_space_available();

	$file_size = filesize( $file['tmp_name'] );
	if ( $space_left < $file_size ) {
		/* translators: %s: Required disk space in kilobytes. */
		$file['error'] = sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) );
	}

	if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
		/* translators: %s: Maximum allowed file size in kilobytes. */
		$file['error'] = sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) );
	}

	if ( upload_is_user_over_quota( false ) ) {
		$file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
	}

	if ( $file['error'] > 0 && ! isset( $_POST['html-upload'] ) && ! wp_doing_ajax() ) {
		wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
	}

	return $file;
}

Changelog

Version Description
3.0.0 Introduced.