Browse Source

add rename service

feature/new_bilibili_and_instagram_sxs20191126
shixuesen 4 years ago
parent
commit
8c4591cf90
1 changed files with 44 additions and 0 deletions
  1. +44
    -0
      app/Services/RenameService.php

+ 44
- 0
app/Services/RenameService.php View File

@ -128,4 +128,48 @@ class RenameService
} }
} }
public function splitCustomSizeOfFolder($dir = "", $prefix = "", $size = 500)
{
$files = $this->recordAllFiles($dir, $prefix);
$allFileNum = count($files);
$folderNum = ceil(count($files) / $size);
for ($i=0; $i < $folderNum; $i++) {
# code...
$currentDirName = $dir . DIRECTORY_SEPARATOR . $prefix . "_00" .$i;
if (!is_dir($currentDirName)) {
mkdir($currentDirName);
}
for ($j=0 + $i * $size; $j < ($i + 1) * $size && $j < $allFileNum; $j++) {
$fileInfo = pathinfo($files[$j]);
if (is_file($currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename'])) {
echo "file " . $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename'] . " already exists\n";
echo "now rename {$files[$j]} to " . $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['filename'] . "_1." . $fileInfo['extension'] . "\n";
rename($files[$j], $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['filename'] . "_1." . $fileInfo['extension']);
} else{
rename($files[$j], $currentDirName . DIRECTORY_SEPARATOR . $fileInfo['basename']);
}
}
}
}
public function recordAllFiles($dir = "", $prefix) {
$trueFiles = [];
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file) {
if ($file == "." || $file == ".." || $file == ".DS_Store" || $file == ".tmp.drivedownload" || str_contains($file, $prefix . "_00")) {
continue;
}
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
$trueFiles = array_merge($trueFiles, $this->recordAllFiles($dir . DIRECTORY_SEPARATOR . $file, $prefix));
}
if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
$trueFiles[] = $dir . DIRECTORY_SEPARATOR . $file;
}
}
}
// dump($files);
return $trueFiles;
}
} }

Loading…
Cancel
Save