Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Created July 13, 2015 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save georgestephanis/d15364d5cc422be422cb to your computer and use it in GitHub Desktop.
Save georgestephanis/d15364d5cc422be422cb to your computer and use it in GitHub Desktop.
A PHP function to recursively get the files and hashes of all files in a given directory and in its subdirectories.
<?php
function get_all_file_hashes( $directory, $relative_to = null ) {
$directory = rtrim( $directory, '/\\' ) . '/';
$relative_to = $relative_to ? $relative_to : $directory;
$preg_quote_relative = preg_quote( $relative_to, '#' );
$files = glob( $directory . '*' );
$return = array();
foreach ( $files as $file ) {
if ( is_dir( $file ) ) {
$return = array_merge( $return, get_all_file_hashes( $file, $relative_to ) );
} else {
$local_path = preg_replace( '#^' . $preg_quote_relative . '#', '', $file );
$return[ $local_path ] = md5_file( $file );
}
}
return $return;
}
@georgestephanis
Copy link
Author

Usage example:

print_r( get_all_file_hashes( getcwd() ) );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment