最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uploads - Why does get_dirsize return the same size?

programmeradmin4浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 3

Under 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.

发布评论

评论列表(0)

  1. 暂无评论