Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Last active April 6, 2019 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/5b13eaed7e9ca33af5f9e32ba853301f to your computer and use it in GitHub Desktop.
Save ryanbekabe/5b13eaed7e9ca33af5f9e32ba853301f to your computer and use it in GitHub Desktop.
PHP Hash MD5 SHA256 File Recursive
<?php
//bekabeipa@gmail.com
if(isset($_GET['recursivehash']) AND isset($_GET['rest']))
{
//https://stackoverflow.com/questions/34190464/php-scandir-recursively
//https://stackoverflow.com/questions/15687363/php-access-global-variable-in-function
//global $varrest;
date_default_timezone_set('Asia/Jakarta');
$waktupengiriman=date("YmdHi");
$varpathtorecursive = ($_GET['recursivehash']);
$varrest = $_GET['rest']."-".$waktupengiriman;
function scandir_rec($root)
{
global $varrest;
// if root is a file
if (is_file($root)) {
echo '<li>' . basename($root) . '</li>';
return;
}
if (!is_dir($root)) {
return;
}
$dirs = scandir($root);
foreach ($dirs as $dir) {
if ($dir == '.' || $dir == '..') {
continue;
}
$path = $root . '/' . $dir;
if (is_file($path)) {
// if file, create list item tag, and done.
$varmd5=hash_file('md5',$path);
$varsha256=hash_file('sha256',$path);
$varsize=filesize($path);
$vartipe=filetype($path);
echo '<li>file MD5 : '.$varmd5.' - SHA256 : '.$varsha256.' - path - '.$path.' :' . $dir . '</li>';
//mysql_query("INSERT INTO ryanfilelog(id, filename, md5, sha256, filesize, filetype, rest) VALUES(NULL,'$dir','$varmd5','$varsha256','$varsize','$vartipe','$varrest')");
} else if (is_dir($path)) {
// if dir, create list item with sub ul tag
echo '<li>';
echo '<label>' . $dir . '</label>';
echo '<ul>';
scandir_rec($path); // <--- then recursion
echo '</ul>';
echo '</li>';
}
}
}
// init call
$rootDir = $varpathtorecursive;
echo '<ul>';
scandir_rec($rootDir);
echo '</ul>';
die;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment