-
-
Save azaozz/7d29a2505557668ed0e948139cf2b58b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'plugins_loaded', '_my_dir_test' ); | |
function _my_dir_test() { | |
// dir size test | |
$start_execution_time = microtime( true ); | |
$max_execution_time = 20; | |
$uploads_dir = wp_upload_dir(); | |
for ( $i = 0; $i < 20; $i++ ) { | |
$calculated_size = _my_get_directory_size( ABSPATH, $max_execution_time, $start_execution_time ); | |
} | |
echo 'RecursiveDirectoryIterator ABSPATH size 20 times: t = ' . ( microtime( true ) - $start_execution_time ) . ', size = ' . $calculated_size . '<br>'; | |
$start_execution_time = microtime( true ); | |
for ( $i = 0; $i < 20; $i++ ) { | |
$calculated_size = recurse_dirsize( ABSPATH, null, $max_execution_time ); | |
} | |
echo 'get_dirsize() ABSPATH size 20 times: t = ' . ( microtime( true ) - $start_execution_time ) . ', size = ' . $calculated_size . '<br>'; | |
exit; | |
} | |
function _my_get_directory_size( $path, $max_execution_time, $start_execution_time ) { | |
$size = 0; | |
foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) ) as $file ) { | |
// Check if the maximum execution time is a value considered "infinite". | |
if ( 0 !== $max_execution_time && -1 !== $max_execution_time ) { | |
$runtime = ( microtime( true ) - $start_execution_time ); | |
// If the script has been running as long, or longer, as it is allowed, return a failure message. | |
if ( $runtime >= $max_execution_time ) { | |
return -1; | |
} | |
} | |
$size += $file->getSize(); | |
} | |
return $size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment