38 lines
1.0 KiB
PHP
38 lines
1.0 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
|
|
use support\Request;
|
|
use Webman\Http\UploadFile;
|
|
|
|
class UploadController
|
|
{
|
|
public function image(Request $request)
|
|
{
|
|
$file = $request->file('file');
|
|
if (!$file || !$file->isValid()) {
|
|
return jsonResponse(null, '未找到文件或文件无效', 400);
|
|
}
|
|
|
|
$ext = strtolower($file->getUploadExtension());
|
|
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
|
|
return jsonResponse(null, '仅支持图片文件', 400);
|
|
}
|
|
|
|
$dir = public_path() . '/upload/images/' . date('Ymd');
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
|
|
$filename = uniqid() . bin2hex(random_bytes(4)) . '.' . $ext;
|
|
$path = $dir . '/' . $filename;
|
|
$file->move($path);
|
|
|
|
$url = '/upload/images/' . date('Ymd') . '/' . $filename;
|
|
|
|
return jsonResponse([
|
|
'url' => $url,
|
|
'name' => $file->getUploadName(),
|
|
], '上传成功');
|
|
}
|
|
}
|