Created
February 1, 2022 02:39
-
-
Save tateren/3a6657172a2e050510c698ec2eaf52e4 to your computer and use it in GitHub Desktop.
同階層にあるファイルを md5 ハッシュでグループ化する
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 | |
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