The function below is intended to return the size of the base directory. Although additional files have been uploaded, it continues to display the same size. What am I missing?
function get_space_used() {
$upload_dir = wp_upload_dir();
$space_used = number_format( get_dirsize( $upload_dir['basedir'] ) / ( 1024 * 1024 ), 1 );
return $space_used;
}
The function below is intended to return the size of the base directory. Although additional files have been uploaded, it continues to display the same size. What am I missing?
function get_space_used() {
$upload_dir = wp_upload_dir();
$space_used = number_format( get_dirsize( $upload_dir['basedir'] ) / ( 1024 * 1024 ), 1 );
return $space_used;
}
Share
Improve this question
edited Mar 15, 2022 at 17:08
Motivated
asked Mar 15, 2022 at 8:39
MotivatedMotivated
2452 silver badges10 bronze badges
2
- @kero you should post that as an answer – Tom J Nowell ♦ Commented Mar 15, 2022 at 10:54
- Just did it @TomJNowell – kero Commented Mar 15, 2022 at 11:02
1 Answer
Reset to default 3Under the hood, get_dirsize()
uses recurse_dirsize()
which uses a transient called dirsize_cache
. Try clearing that transient and check again.
To do so, you can use one of the following methods:
- call
delete_transient('dirsize_cache');
- use WP CLI's
wp transient delete
- use a plugin
Looking further through the source code, I think it should be possible to use recurse_dirsize()
directly in your code and telling it not to cache like so:
$upload_dir = wp_upload_dir();
$size = recurse_dirsize($upload_dir['basedir'], null, null, []);
By passing an empty array []
as the fourth argument, it should circumvent the cache because the !isset($directory_cache)
now returns a different result.