Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get Directory size using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 193
    Comment on it

    Hello Reader's!, If you are looking for a code that can give the directory size from a given path, Then you can use below library code for both Windows and Linux hosting:-

    If on a Windows Host:

    <?
        $filePath = 'c:/www/myfolder';
        $obj = new COM ( 'scripting.filesystemobject' );
        if ( is_object ( $obj ) )
        {
            $ref = $obj->getfolder ( $filePath );
            echo 'Directory: ' . $filePath . ' => Size: ' . $ref->size;
            $obj = null;
        }
        else
        {
            echo 'fail to detect';
        }
    ?>
    

    And if you are using a Linux Host then correct your code as follows:-

    <?
        $filePath = './path/var/myfolder';
        $io = popen ( '/usr/bin/du -sk ' . $filePath, 'r' );
        $size = fgets ( $io, 4096);
        $size = substr ( $size, 0, strpos ( $size, "\t" ) );
        pclose ( $io );
        echo 'Directory: ' . $filePath . ' => Size: ' . $size;
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: