blog/app/controller/FileController.php

31 lines
879 B
PHP
Raw Permalink Normal View History

2024-06-08 15:54:41 +08:00
<?php
namespace app\controller;
use app\util\Result;
use support\Request;
class FileController
{
/**
* @Inject
* @var Result
*/
private $result;
public function upload(Request $request)
{
$file = $request->file('file');
if ($file && $file->isValid()) {
$name = md5(uniqid());
$file->move(public_path() . '/upload/' . $name . '.' . $file->getUploadExtension());
$data = [
'id' => $name,
'src' => $request->header('x-forwarded-proto') ?? 'http' . '://' . $request->host() . '/upload/' . $name . '.' . $file->getUploadExtension(),
'fileName' => $name
];
return json(retData($this->result::SUCCESS, 'upload success', $data));
}
return json(retData($this->result::ERROR, 'file not found'));
}
}