ThinkPHP 3.0 `UploadFile` 類預設將縮略圖直接存於 `thumbPath` 根目錄,不支援 `autoSub` 子目錄規則(如按日期、hash 分目錄),導致單一目錄檔案過多、效能下降。
下載 ThinkPHP 3.1 版 `UploadFile.class.php` 替換舊檔:
https://github.com/liu21st/extend/tree/master/Extend/Library/ORG/Net
getThumbSubName() 方法於 `UploadFile.class.php` 內,模仿既有 `getSubName()` 新增:
private function getThumbSubName($file) {
switch ($this->subType) {
case 'date':
$dir = date($this->dateFormat, time());
break;
case 'hash':
default:
$name = md5($this->thumbPath);
$dir = '';
for ($i = 0; $i < $this->hashLevel; $i++) {
$dir .= $name{$i} . '/';
}
break;
}
$full = $this->thumbPath . $dir;
if (!is_dir($full)) mkdir($full, 0755, true);
return $dir;
}
// 原始
$thumbPath = $this->thumbPath ? $this->thumbPath . ($this->autoSub ? $this->getSubName($file) . '/' : '') : $file['savepath'];
// 修改為
$thumbPath = $this->thumbPath ? $this->thumbPath . ($this->autoSub ? $this->getThumbSubName($file) . '/' : '') : $file['savepath'];
上傳圖片並開啟縮略圖生成,檢查 `thumbPath` 下是否出現 20240115/ 或 a/b/c/ 等子目錄,且縮略圖正確存放其中。