Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <?php
  2.  
  3. $topdir = 'Fable';
  4. $dirs = listDir($topdir, true);
  5.  
  6. foreach ($dirs as $dir) {
  7. $files = listDir($topdir . DIRECTORY_SEPARATOR . $dir);
  8. sort($files);
  9. $total = count($files);
  10. $portion = floor($total / 10);
  11.  
  12. createAdditionals($dir, $topdir);
  13.  
  14. $start = $dir - 1;
  15. foreach ($files as $k => $file) {
  16. $addon = '';
  17. if (!($k % $portion)) {
  18. $start++;
  19.  
  20. }
  21. if ($start < 100) {
  22. $addon = '0';
  23. } else {
  24. $addon = '';
  25. }
  26.  
  27. $from = $topdir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file;
  28. $to = $topdir . DIRECTORY_SEPARATOR . $addon . $start . DIRECTORY_SEPARATOR . $file;
  29.  
  30. if ($from != $to) {
  31. rename($from, $to);
  32. }
  33. }
  34. }
  35.  
  36. function createAdditionals($dir, $topdir)
  37. {
  38. if (substr($dir, -1, 1) == '0') {
  39. for ($i = (int) $dir; $i < $dir + 10; $i++) {
  40. if ($i < 100) {
  41. $addon = '0';
  42. } else {
  43. $addon = '';
  44. }
  45. if (!file_exists($topdir . DIRECTORY_SEPARATOR . $addon . $i)) {
  46. mkdir($topdir . DIRECTORY_SEPARATOR . $addon . $i, 0755);
  47. }
  48. }
  49. }
  50. }
  51.  
  52. function listDir($path, $onlyTop = false)
  53. {
  54. $dirs = [];
  55. if ($handle = opendir($path)) {
  56. while (false !== ($entry = readdir($handle))) {
  57. if ($entry != '.' && $entry != '..') {
  58. if ($onlyTop) {
  59. if (substr($entry, -1, 1) != '0') {
  60. continue;
  61. }
  62. }
  63. $dirs[] = $entry;
  64. }
  65.  
  66. }
  67. closedir($handle);
  68. }
  69. return $dirs;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement