tp6Day7--上传

59 阅读1分钟

配置上传地址 filesystem.php

<?php

return [
    // 默认磁盘
    'default' => env('filesystem.driver', 'local'),
    // 磁盘列表
    'disks'   => [
        'local'  => [
            'type' => 'local',
            'root' => app()->getRootPath() . 'public/storage',
        ],
        'public' => [
            // 磁盘类型
            'type'       => 'local',
            // 磁盘路径
            'root'       => app()->getRootPath() . 'public/storage',
            // 磁盘路径对应的外部URL路径
            'url'        => '/storage',
            // 可见性
            'visibility' => 'public',
        ],
        // 更多的磁盘配置信息
    ],
];

具体代码

<?php
/**
 * @description: TODO
 * @author wuJiaWei
 * @date 2024/6/28 17:24:27
 * @version 1.0
 */

namespace app\controller;

use think\facade\Filesystem;
use think\facade\Request;

class Upload
{
    public function upload()
    {
        //获取表单的上传数据
        $file = Request::file('image');
        $info = Filesystem::putFile('topic',$file,'md5');
        // 构建文件访问的URL
        $filePath = Filesystem::getDiskConfig('public', 'url') . '/' . $info;

        return json(['status' => 'success', 'file_path' => $filePath]);
    }
}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://tp6.com/upload/upload" enctype="multipart/form-data" method="post">
    <input type="file" name="image">
    <input type="submit" value="确定">
</form>
</body>
</html>

image.png

image.png