Skip to content

Instantly share code, notes, and snippets.

@craigedmonds
Last active October 11, 2017 10:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save craigedmonds/212dd0e0b26a1c96934d951507295c38 to your computer and use it in GitHub Desktop.
Iterate through directories and sub directories and get list of files and folders in an array
<?php
###########################
/*
This script is useful if you wanted to get a list of the files and folders within a directory on your web site.
EG: Lets say you wanted to monitor your theme folder in wordpress. With this script you can generate a list of
all the files and folders and check for any changes.
You could extend this script to exclude certain folders or file names or file types.
Written by craig@123marbella.com on 11th of October 2017
*/
###########################
//SETTINGS
###########################
//define the path you wish to check
$path = "/home/username/public_html/wp-content/themes/mywordpress_theme";
###########################
//get a list of directories and sub directories
//within the $path
###########################
function get_list_of_directories($path) {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
//add the $path to the array as we need to scan for files in $path
$array_of_dirs[] = $path;
//scan for folder and sub folders
$paths = array($path);
foreach ($iter as $path => $dir) {
if ($dir->isDir()) {
$array_of_dirs[] = $path;
}
}
return $array_of_dirs;
}
###########################
//STEP 1: get an array of folders for scanning
###########################
$array_of_directories = get_list_of_directories($path);
###########################
//STEP 2: loop through the $array_of_directories
###########################
foreach($array_of_directories as $directory) {
$files = scandir($directory);
//loop through the files in the directory
foreach($files as $file) {
if($file == ".") { continue; }
if($file == "..") { continue; }
$path_to_file = $directory . "/" . $file;
$file_size = filesize($path_to_file);
$file_size_kb = $file_size / 1028;
$last_modified_date_unix = filemtime ($path_to_file);
$last_modified_date = gmdate("Y-m-d H:i:s", $last_modified_date_unix);
$check_sum = md5_file($path_to_file);
//get fileinfo
$ext = pathinfo($file);
$file_ext = $ext['extension'];
$output_array[] = array(
'path' => $directory,
'file_name' => $file,
'file_ext' => $file_ext,
'path_to_file' => $path_to_file,
'file_size_bytes' => $file_size,
'file_size_kb' => $file_size_kb,
'last_modified_date_unix' => $last_modified_date_unix,
'last_modified_date' => $last_modified_date,
'check_sum' => $check_sum,
);
}
}
$total_folders = count($array_of_directories);
$total_files = count($output_array);
$final_array = array(
'check_date' => gmdate('Y-m-d H:i:s'),
'total_folders' => $total_folders,
'total_files' => $total_files,
'folders' => $array_of_directories,
'files' => $output_array
);
###########################
//FINAL STEP: output the array
###########################
echo "<pre>";
print_r($final_array);
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment