<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;

class Upload extends AdminBaseController
{

    /**
     * 文件上传
     */
    public function file()
    {
        $file     = request()->file("file");
        $savename = \think\facade\Filesystem::disk('public')->putFile('files', $file);
        if ($file) {
            $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
            ajax_return(0, '', [
                'src'   => request()->domain() . "/storage/" . str_replace("\\", "/", $savename),
                'path'  => "./storage/" . str_replace("\\", "/", $savename),
                'title' => $filename,
            ]);
        } else {
            ajax_return(1, '上传失败，请稍后重试');
        }
    }

    /**
     * 图片上传
     */
    public function image()
    {
        $file     = request()->file("file");
        $savename = \think\facade\Filesystem::disk('public')->putFile('images', $file);
        if ($file) {
            $filename = str_replace(strrchr($_FILES['file']['name'], "."), "", $_FILES['file']['name']);
            ajax_return(0, '', [
                'src'   => request()->domain() . "/storage/" . str_replace("\\", "/", $savename),
                'title' => $filename,
            ]);
        } else {
            ajax_return(1, '上传失败，请稍后重试');
        }
    }
}