函数文档

get_dirsize()

💡 云策文档标注

概述

get_dirsize() 是一个辅助函数,用于获取目录的大小,主要用于检查博客是否超出允许的上传空间限制。

关键要点

  • 函数接受两个参数:$directory(必需,目录的完整路径)和 $max_execution_time(可选,超时时间,单位为秒)。
  • 返回值:有效目录时返回字节大小,无效目录时返回 false,超时时返回 null。
  • 在 Multisite 环境中,如果是主站点,会排除子站点目录以避免重复计算。

代码示例

function get_dirsize( $directory, $max_execution_time = null ) {
    if ( is_multisite() && is_main_site() ) {
        $size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
    } else {
        $size = recurse_dirsize( $directory, null, $max_execution_time );
    }
    return $size;
}

注意事项

  • 超时时间是全局的,从 WordPress 开始加载时计算。
  • 函数内部调用 recurse_dirsize() 进行递归计算。

📄 原文内容

Gets the size of a directory.

Description

A helper function that is used primarily to check whether a blog has exceeded its allowed upload space.

Parameters

$directorystringrequired
Full path of a directory.
$max_execution_timeintoptional
Maximum time to run before giving up. In seconds.
The timeout is global and is measured from the moment WordPress started to load.

Default:null

Return

int|false|null Size in bytes if a valid directory. False if not. Null if timeout.

Source

function get_dirsize( $directory, $max_execution_time = null ) {

	/*
	 * Exclude individual site directories from the total when checking the main site of a network,
	 * as they are subdirectories and should not be counted.
	 */
	if ( is_multisite() && is_main_site() ) {
		$size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
	} else {
		$size = recurse_dirsize( $directory, null, $max_execution_time );
	}

	return $size;
}

Changelog

Version Description
MU (3.0.0) MU (3.0.0)
5.2.0 Introduced.

User Contributed Notes