tp6实现文件上传接口(单文件上传)

2022-05-26 08:23:14 浏览数 (1)

返回给前端的接口格式:

路由:

代码语言:javascript复制
<?php
//  ----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
//  ----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
//  ----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
//  ----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
//  ----------------------------------------------------------------------
use thinkfacadeRoute;


Route::post('goods/upload_goods', 'Goods/uploadGoods');

sql:

代码语言:javascript复制
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2022-05-25 12:57:32
-- 服务器版本: 5.7.26
-- PHP 版本: 7.3.4

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = " 00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- 数据库: `kkk`
--

-- --------------------------------------------------------

--
-- 表的结构 `goods`
--

CREATE TABLE `goods` (
  `id` int(10) UNSIGNED NOT NULL,
  `goods_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `goods_img` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `cat_id` int(11) NOT NULL,
  `goods_price` decimal(8,2) NOT NULL,
  `is_show` tinyint(4) NOT NULL,
  `goods_introduce` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 转存表中的数据 `goods`
--

INSERT INTO `goods` (`id`, `goods_name`, `goods_img`, `cat_id`, `goods_price`, `is_show`, `goods_introduce`) VALUES
(2, '纤伊寻雪纺连衣裙女2020春夏新款韩版碎花裙子时尚V领很仙的小个子连衣裙 白花色 L', '/storage/goods/f6/655e2bed6b7bea027fb69a3c11a27c.jpg', 8, '0.01', 1, '  <p>商品详情</p><p><img src="/storage/goods/ed\e51d8a0f3fad9ddea11880e2903ab1.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/9e\3a9cb6f37f31b35076c1bb6cfab249.jpg" style="max-width:100%;"></p>'),
(3, '法国KJ蕾丝聚拢文胸套装刺绣薄款无钢圈插片式调整型小胸内衣舒适透气文胸 聚拢虾粉色1 75B=34B(配内裤)', '/storage/goods/84\f9e13fa3a6093990bf3de4e2da74a0.jpg', 8, '0.01', 1, ' <p>商品详情</p><p><img src="/storage/goods/13\2ad82b0d415a8dcab51e525925212a.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/80\a81fa79b6a0f0f84ba705cb31aa32d.jpg" style="max-width:100%;"></p><p><br></p>'),
(4, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/f6\655e2bed6b7bea027fb69a3c11a27c.jpg', 10, '56.00', 1, '<p>商品详情</p><p><img src="/storage/goods/63\ebb026ad52963f53e0ae851d6e42f7.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/ed\e51d8a0f3fad9ddea11880e2903ab1.jpg" style="max-width:100%;"></p>'),
(5, '纤伊寻雪纺连衣裙女2020春夏新款韩版碎花裙子时尚V领很仙的小个子连衣裙 白花色 L', '/storage/goods/84\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '89.00', 1, '<p>商品详情</p><p><img src="/storage/goods/80\a81fa79b6a0f0f84ba705cb31aa32d.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/9e\3a9cb6f37f31b35076c1bb6cfab249.jpg" style="max-width:100%;"></p>'),
(6, 'Redmi 8 5000mAh大电量 大字体大音量大内存 3D四曲面机身 AI双摄 骁龙八核处理器 AI人脸解锁 3GB 32GB 碳岩灰 游戏智能手机 小米', '/storage/goods/b4\a403d9874ed426bd5210a2f42c33f0.jpg', 11, '789.00', 1, '<p>商品详情</p><p><img src="/storage/goods/63\ebb026ad52963f53e0ae851d6e42f7.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/f7\b1f9e85e6b945be48e174f9260b909.jpg" style="max-width:100%;"></p>'),
(7, 'OUHEN轻奢品牌女装 小个子印花连衣裙女2020夏季新款遮肚减龄气质时尚收腰显瘦短款裙子 白色黑花 160/M', '/storage/goods/84\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '56.00', 1, '<p>商品详情</p><p><img src="/storage/goods/ed\e51d8a0f3fad9ddea11880e2903ab1.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/6f\9c4f90bae503b687b9776fd2170e93.jpg" style="max-width:100%;"></p>'),
(8, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/84\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '345.00', 1, '<p>商品详情</p><p><img src="/storage/goods/13\2ad82b0d415a8dcab51e525925212a.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/6f\9c4f90bae503b687b9776fd2170e93.jpg" style="max-width:100%;"></p>'),
(9, '绣球花盆栽室内外阳台绿植花卉绣球花苗庭院地栽植物变色无尽夏重瓣八仙花苗四季种植循环开花耐寒幸运花 无尽夏 2年苗', '/storage/goods/b4\a403d9874ed426bd5210a2f42c33f0.jpg', 11, '89.00', 1, '<p>商品详情</p><p><img src="/storage/goods/ed\e51d8a0f3fad9ddea11880e2903ab1.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/63\ebb026ad52963f53e0ae851d6e42f7.jpg" style="max-width:100%;"></p>'),
(10, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/84\f9e13fa3a6093990bf3de4e2da74a0.jpg', 11, '899.00', 1, '<p>商品详情</p><p><img src="/storage/goods/ed\e51d8a0f3fad9ddea11880e2903ab1.jpg" style="max-width:100%;"></p><p><img src="/storage/goods/9e\3a9cb6f37f31b35076c1bb6cfab249.jpg" style="max-width:100%;"></p>'),
(11, '666', '/storage/goods/f6\9f50aed16fc629c65f6526b017296a.png', 1, '666.00', 1, '');

--
-- 转储表的索引
--

--
-- 表的索引 `goods`
--
ALTER TABLE `goods`
  ADD PRIMARY KEY (`id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `goods`
--
ALTER TABLE `goods`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

控制器:

代码语言:javascript复制
use thinkRequest;
use thinkfacadeView;
use thinkfacadeDb;
public function uploadGoods(Request $request){
	
		$file = $request->file('image');
		$savename = thinkfacadeFilesystem::putFile( 'goods', $file,'md5');
		
		return json(['code'=>1, 'msg'=>'上传成功', 'data'=> ['url'=>['/storage/'.$savename]]]);
	}

postman:

config/filesystem.php文件。粘贴复制

代码语言:javascript复制
<?php

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

0 人点赞