recurse_dirsize()WP 3.0.0

Рекурсивно получает размер директории в байтах. Т.е. можно указать родительскую папку и получить полные её размер: включая вложенные папки.

С WP 5.6. результат кэшируется во временную опцию: get_transient( 'dirsize_cache' ).

Является основой для однотипной функции get_dirsize().

Для работы функции может быть понадобится подключить файл:

require_once ABSPATH . WPINC .'/functions.php ';
Основа для: get_dirsize()
Хуки из функции

Возвращает

int|false|null. Размер в байтах. Если директории не существует то вернет fales.

Использование

recurse_dirsize( $directory, $exclude, $max_execution_time, $directory_cache );
$directory(строка) (обязательный)
Полный путь до каталога (папки).
$exclude(строка)
Полный путь до вложенных папок, размер которых не нужно учитывать.
По умолчанию: null
$max_execution_time(число) (WP 5.2)
Максимальное время выполнения в секундах. Время считается начиная от глобальной точки отсчета (когда WP только начал загружаться - константа WP_START_TIMESTAMP).
По умолчанию: null - ini_get( 'max_execution_time' )
& $directory_cache(массив) (WP 5.6)
Массив путей директорий, размер которых кэшируется. Используется самой функцией во время рекурсии.

Примеры

0

#1 Получим полный размер каталога загрузок uploads

$upload_dir = (object) wp_upload_dir();

// подключим файл функции
require_once ABSPATH . WPINC .'/ms-functions.php';

$mb = recurse_dirsize( $upload_dir->basedir );

echo number_format( $mb / (1024*1024), 1 ) .' MB'; // выведет: 74.5 MB

Список изменений

С версии 3.0.0 Введена.
С версии 4.3.0 The $exclude parameter was added.
С версии 5.2.0 The $max_execution_time parameter was added.
С версии 5.6.0 The $directory_cache parameter was added.

Код recurse_dirsize() WP 6.5.2

function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) {
	$directory  = untrailingslashit( $directory );
	$save_cache = false;

	if ( ! isset( $directory_cache ) ) {
		$directory_cache = get_transient( 'dirsize_cache' );
		$save_cache      = true;
	}

	if ( isset( $directory_cache[ $directory ] ) && is_int( $directory_cache[ $directory ] ) ) {
		return $directory_cache[ $directory ];
	}

	if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
		return false;
	}

	if (
		( is_string( $exclude ) && $directory === $exclude ) ||
		( is_array( $exclude ) && in_array( $directory, $exclude, true ) )
	) {
		return false;
	}

	if ( null === $max_execution_time ) {
		// Keep the previous behavior but attempt to prevent fatal errors from timeout if possible.
		if ( function_exists( 'ini_get' ) ) {
			$max_execution_time = ini_get( 'max_execution_time' );
		} else {
			// Disable...
			$max_execution_time = 0;
		}

		// Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value.
		if ( $max_execution_time > 10 ) {
			$max_execution_time -= 1;
		}
	}

	/**
	 * Filters the amount of storage space used by one directory and all its children, in megabytes.
	 *
	 * Return the actual used space to short-circuit the recursive PHP file size calculation
	 * and use something else, like a CDN API or native operating system tools for better performance.
	 *
	 * @since 5.6.0
	 *
	 * @param int|false            $space_used         The amount of used space, in bytes. Default false.
	 * @param string               $directory          Full path of a directory.
	 * @param string|string[]|null $exclude            Full path of a subdirectory to exclude from the total,
	 *                                                 or array of paths.
	 * @param int                  $max_execution_time Maximum time to run before giving up. In seconds.
	 * @param array                $directory_cache    Array of cached directory paths.
	 */
	$size = apply_filters( 'pre_recurse_dirsize', false, $directory, $exclude, $max_execution_time, $directory_cache );

	if ( false === $size ) {
		$size = 0;

		$handle = opendir( $directory );
		if ( $handle ) {
			while ( ( $file = readdir( $handle ) ) !== false ) {
				$path = $directory . '/' . $file;
				if ( '.' !== $file && '..' !== $file ) {
					if ( is_file( $path ) ) {
						$size += filesize( $path );
					} elseif ( is_dir( $path ) ) {
						$handlesize = recurse_dirsize( $path, $exclude, $max_execution_time, $directory_cache );
						if ( $handlesize > 0 ) {
							$size += $handlesize;
						}
					}

					if ( $max_execution_time > 0 &&
						( microtime( true ) - WP_START_TIMESTAMP ) > $max_execution_time
					) {
						// Time exceeded. Give up instead of risking a fatal timeout.
						$size = null;
						break;
					}
				}
			}
			closedir( $handle );
		}
	}

	if ( ! is_array( $directory_cache ) ) {
		$directory_cache = array();
	}

	$directory_cache[ $directory ] = $size;

	// Only write the transient on the top level call and not on recursive calls.
	if ( $save_cache ) {
		$expiration = ( wp_using_ext_object_cache() ) ? 0 : 10 * YEAR_IN_SECONDS;
		set_transient( 'dirsize_cache', $directory_cache, $expiration );
	}

	return $size;
}