31 lines
879 B
PHP
31 lines
879 B
PHP
<?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'));
|
|
}
|
|
} |