Created
July 13, 2015 19:53
-
-
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.
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
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: