public function file() {
import('ORG.Net.UploadFile');
import('ORG.Util.Services_JSON');
$upload = new UploadFile();
$upload->maxSize = 3145728; // 3MB
$upload->allowExts = ['jpg', 'gif', 'png', 'jpeg'];
$upload->savePath = './uploads/Images/';
$info = $upload->uploadOne($_FILES['imgFile']);
if ($info) {
$url = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']))
. '/uploads/Images/' . $info[0]['savename'];
echo (new Services_JSON())->encode(['error' => 0, 'url' => $url]);
} else {
$this->error($upload->getErrorMsg());
}
}
public function file_manager() {
import('ORG.Util.Services_JSON');
$root = './uploads/Images/';
$exts = ['gif', 'jpg', 'jpeg', 'png', 'bmp'];
$dir = $_GET['dir'] ?? '';
$path = $_GET['path'] ?? '';
$order = $_GET['order'] ?? 'name';
// 安全檢查
if (!in_array($dir, ['', 'image', 'flash', 'media', 'file'])) exit('Invalid dir');
if (strpos($path, '..') !== false) exit('Access denied');
$current = realpath($root . $dir . $path) . '/';
if (!is_dir($current)) exit('Directory not exist');
$files = [];
foreach (scandir($current) as $fn) {
if ($fn[0] === '.') continue;
$full = $current . $fn;
$isDir = is_dir($full);
$files[] = [
'is_dir' => $isDir,
'filename' => $fn,
'filesize' => $isDir ? 0 : filesize($full),
'is_photo' => !$isDir && in_array(strtolower(pathinfo($fn, PATHINFO_EXTENSION)), $exts),
'datetime' => date('Y-m-d H:i:s', filemtime($full))
];
}
usort($files, function($a, $b) use ($order) {
return strcmp($a[$order], $b[$order]);
});
echo (new Services_JSON())->encode([
'moveup_dir_path' => dirname($path),
'current_dir_path' => $path,
'current_url' => '/uploads/Images/' . $dir . $path,
'total_count' => count($files),
'file_list' => $files
]);
}
搭配 KindEditor / CKEditor 的 filebrowserBrowseUrl 指向 file_manager,即可實現圖片瀏覽、選取、上傳一站式體驗。