check_upload_size

函数
check_upload_size ( $file )
参数
  • (array) $file An element from the `$_FILES` array for a given file.
    Required:
返回值
  • (array) The `$_FILES` array element with 'error' key set if file exceeds quota. 'error' is empty otherwise.
定义位置
相关方法
check_upload_mimeswp_max_upload_sizemedia_upload_filemedia_upload_videomedia_upload_image
引入
3.0.0
弃用
-

check_upload_size: 这个函数检查一个文件的大小是否在允许的上传限制之内。它用于防止用户上传过大的文件,以免损害网站的性能。

确定上传的文件是否超过了空间配额。

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;
}

常见问题

FAQs
查看更多 >