Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there any way to find out how much of disk space has been used by Media Library? My blog's media files stored in a separate folder named "media" rather than the default "wp-content/uploads".

Thanks

share|improve this question
    
Most hostings provide some space consumption info in some form in their administration panels (since they limit space provided). If you want this in and via WordPress please elaborate on details a bit. –  Rarst Oct 11 '12 at 17:35
add comment

2 Answers 2

up vote 2 down vote accepted

Using this StackOverflow Answer, I came up with a Dashboard Widget that displays the following:
Folder Sizes Dashboard Widget

I extended the OP request for the uploads dir size (default or custom location) and added wp-content (default or custom), blogs.dir case in Multisite, and WordPress base dir.

<?php
/*
Plugin Name: Folder Sizes Dashboard Widget
Plugin URI: http://wordpress.stackexchange.com/q/67876/12615
Description: List the following folder sizes in a Dashboard Widget: Uploads dir, WP Content dir, WordPress base dir. 
Observation: PHP folder size functions from this Answer: http://stackoverflow.com/a/8348396/1287812
*/

add_action( 'wp_dashboard_setup', 'wpse_67876_wp_dashboard_setup' );

function wpse_67876_wp_dashboard_setup() 
{
    // Admins only
    if( current_user_can( 'install_plugins' ) )
        wp_add_dashboard_widget( 'wpse_67876_folder_sizes', __( 'Folder Sizes' ), 'wpse_67876_wp_add_dashboard_widget' );
}

function wpse_67876_wp_add_dashboard_widget() 
{
    $upload_dir     = wp_upload_dir(); 
    $upload_space   = wpse_67876_foldersize( $upload_dir['basedir'] );
    $content_space  = wpse_67876_foldersize( WP_CONTENT_DIR );
    $wp_space       = wpse_67876_foldersize( ABSPATH );

    /* ABSOLUTE paths not being shown in Widget */

    // echo '<b>' . $upload_dir['basedir'] . ' </b><br />';
    echo '<i>Uploads</i>: ' . wpse_67876_format_size( $upload_space ) . '<br /><br />';

    // echo '<b>' . WP_CONTENT_DIR . ' </b><br />';
    echo '<i>wp-content</i>: ' . wpse_67876_format_size( $content_space ) . '<br /><br />';  

    if( is_multisite() )
    {
        echo '<i>wp-content/blogs.dir</i>: ' . wpse_67876_format_size( wpse_67876_foldersize( WP_CONTENT_DIR . '/blogs.dir' ) ) . '<br /><br />';  
    }

    // echo '<b>' . ABSPATH . ' </b><br />';
    echo '<i>WordPress</i>: ' . wpse_67876_format_size( $wp_space );    
}



function wpse_67876_foldersize( $path ) 
{
    $total_size = 0;
    $files = scandir( $path );
    $cleanPath = rtrim( $path, '/' ) . '/';

    foreach( $files as $t ) {
        if ( '.' != $t && '..' != $t ) 
        {
            $currentFile = $cleanPath . $t;
            if ( is_dir( $currentFile ) ) 
            {
                $size = wpse_67876_foldersize( $currentFile );
                $total_size += $size;
            }
            else 
            {
                $size = filesize( $currentFile );
                $total_size += $size;
            }
        }   
    }

    return $total_size;
}

function wpse_67876_format_size($size) 
{
    $units = explode( ' ', 'B KB MB GB TB PB' );

    $mod = 1024;

    for ( $i = 0; $size > $mod; $i++ )
        $size /= $mod;

    $endIndex = strpos( $size, "." ) + 3;

    return substr( $size, 0, $endIndex ) . ' ' . $units[$i];
}

PS: I suppose the foldersize function is optimized for performance as asked by StackOverflow OP, but am not sure...

share|improve this answer
add comment

I have used the same recursive method myself. It adds up the space used and counts the number of files nicely. But on the WP-MU site that I am managing many of the users have allocated disk space up to and over 1gB and over 10,000 files. To be able to charge the right sum my system has to perform periodic disk scans and on these big accounts it appears that the processing exceeds my ISPs processing limits. So when the tight recursive loop has finished the process bails out and skips the little finishing part where blog id, space and date are supposed to be stored in the usage log. My only option then is to implement a 'bit by bit' - logic, or upgrade my web hotel. That is expensive, and the first option requires programming of a kind that I would like to avoid.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.