在ThinkPHP6.0中,如何实现文件上传,并且不存储到public目录,因为public目录下的文件,默认情况下,是不带登录验证控制的,如果有敏感文件,不允许匿名访问,就可以使用下面的方法
在Controller中写类似代码:
public function upload() {
if (!session('user')) {
$this->ajaxReturn(["auth" => "NoAuth"]);
}
try {
$file = request()->file('file');
validate(['file' => ['fileSize' => 102400, 'fileExt' => 'gif,jpg,png,jpeg']])->check(['file' => $file]);
$savename = \think\facade\Filesystem::putFile('image', $file);
$url = "/file?url=".$savename;
$ret['result'] = "OK";
$ret['url'] = $url;
$this->ajaxReturn($ret);
} catch (\Exception $e) {
$this->ajaxReturn(["result" => "error", "msg" => $e->getMessage()]);
}
}
public function file($url) {
if (!session('user')) {
$this->ajaxReturn(["auth" => "NoAuth"]);
}
$file = \think\facade\Filesystem::path($url);
return download($file);
}