Skip to content

Instantly share code, notes, and snippets.

@tateren
Created February 1, 2022 02:39
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 tateren/3a6657172a2e050510c698ec2eaf52e4 to your computer and use it in GitHub Desktop.
Save tateren/3a6657172a2e050510c698ec2eaf52e4 to your computer and use it in GitHub Desktop.
同階層にあるファイルを md5 ハッシュでグループ化する
<?php
declare(strict_types=1);
foreach (new DirectoryIterator('./') as $fileInfo) {
$filename = $fileInfo->getFilename();
if ($fileInfo->isDot() || $fileInfo->isDir() || $filename === basename(__FILE__)) {
continue;
}
$md5Hash = md5_file($filename);
if (!file_exists($md5Hash) && !mkdir($md5Hash) && !is_dir($md5Hash)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $md5Hash));
}
rename($filename, $md5Hash . DIRECTORY_SEPARATOR . $filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment