PHP如何将8500文件分成文件夹

Mui*_*ter 1 php

我有大约8500个PDF文件存储在我的服务器上.我想在子文件夹中像这样潜水:

  • 0 - 100>文件夹1
  • 101 - 200>文件夹2
  • 201 - 300>文件夹3
  • ...
  • 1001 - 1100>文件夹11
  • 1101 - 1200>文件夹12
  • 1201 - 1300>文件夹13
  • ...

我知道如何创建一个新目录并在文件夹之间移动文件.但是检测哪些文件夹放置它们的最佳方法是什么.第二,如何使用正确的if函数来查看它们是否存在于某个文件夹中.

Fre*_*nke 5

这样的东西可能有效(未经测试)

<?php
$listOfFiles = ["100.pdf", "200.pdf", "201.pdf"]; // populate this using glob / opendir

foreach ($listOfFiles as $file) {

    // intval will return just the number part of the filename
    // division with 100 + ceil will make 100 = 1, 200 = 2, 201 = 3 aso
    $folderName = ceil(intval($file) / 100);

    // make folder
    if (!file_exists($folderName)) {
        mkdir($folderName, 0755, true);
    }

    // move file if does not exists
    if (!file_exists($foldername."/".$file)) {
        rename($file, $foldername."/".$file);
    } else {
        // maybe delete it
        unlink($file);
    }

}
Run Code Online (Sandbox Code Playgroud)