search
HomePHP FrameworkThinkPHPEncapsulating ThinkPHP6.0 universal file upload

Encapsulating ThinkPHP6.0 universal file upload

May 04, 2020 am 11:40 AM
thinkphpdocument

本文实例讲述了封装ThinkPHP6通用文件上传方法,上传功能使用的是LayUI的upload组件。

封装ThinkPHP6.0通用文件上传教程

一、打开项目在config文件夹下创建upload.php配置文件用来管理文件上传的后缀和大小

<?php

return [
    //定义允许上传文件后缀的数组
    &#39;suffix_arr&#39;    =>  [
        //允许图片上传的后缀
        &#39;image&#39;     =>  &#39;jpg,jpeg,png,gif&#39;,
        //允许上传文件的后缀
        &#39;file&#39;      =>  &#39;zip,gz,doc,txt,pdf,xls&#39;,
        //...
    ],
    //定义允许上传文件大小的数组
    &#39;size_arr&#39;      =>  [
        //允许图片上传的大小
        &#39;image&#39;     =>  10,
        //允许文件上传的大小
        &#39;file&#39;      =>  50
    ],
];

二、修改config\filesystem.php配置文件配置上传根目录及上传规则

<?php

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

三、 在app\controller目录下创建Upload.php类并编写upload()文件上传方法

<?php

namespace app\controller;

use think\exception\ValidateException;

class Upload
{
    //上传
    public function upload()
    {
        //判断是否是POST请求,如果是处理上传逻辑
        if (request()->isPost()){

            //接收文件上传类型
            $type = request()->param(&#39;type&#39;,&#39;&#39;,&#39;trim&#39;);
            $name = request()->param(&#39;name&#39;,&#39;&#39;,&#39;trim&#39;);
            //获取表单上传文件
            $file = request()->file(&#39;file&#39;);
            //组装文件保存目录
            $upload_dir = &#39;/&#39;.$type.&#39;/&#39;.$name;

            try {
                //从config/upload.php配置文件中读取允许上传的文件后缀和大小
                $suffix_config = config(&#39;upload.suffix_arr&#39;);
                $size_config = config(&#39;upload.size_arr&#39;);

                if (empty($size_config[$type]) || empty($size_config[$type])){
                    return false;
                }else{
                    $suffix = $suffix_config[$type];
                    $size = $size_config[$type];
                }
                //验证器验证上传的文件
                validate([&#39;file&#39;=>[
                    //限制文件大小
                    &#39;fileSize&#39;      =>  $size * 1024 * 1024,
                    //限制文件后缀
                    &#39;fileExt&#39;       =>  $suffix
                ]],[
                    &#39;file.fileSize&#39; =>  &#39;上传的文件大小不能超过&#39;.$size.&#39;M&#39;,
                    &#39;file.fileExt&#39;  =>  &#39;请上传后缀为:&#39;.$suffix.&#39;的文件&#39;
                ])->check([&#39;file&#39;=>$file]);

                //上传文件到本地服务器
                $filename = \think\facade\Filesystem::disk(&#39;public&#39;)->putFile($upload_dir, $file);
                if ($filename){
                    $src = &#39;/uploads/&#39;.$filename;
                    return json([&#39;code&#39;=>1,&#39;result&#39;=>$src]);
                }else{
                    return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;上传失败&#39;]);
                }
            }catch (ValidateException $e){
                return json([&#39;code&#39;=>0,&#39;msg&#39;=>$e->getMessage()]);
            }
        }else{
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;非法请求&#39;]);
        }
    }
}

四、 打开app\controller\Index.php类并修改index方法

<?php

namespace app\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index()
    {
        //渲染前端页面
        return view();
    }
}

五、在app\view\index目录下创建index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <!-- layui.css 推荐使用本地文件 -->
    <link rel="stylesheet" href="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.4/css/layui.css"  media="all">
    <style>
        #avatar_thumb {
            position: absolute;
            left: 50%;top: 50%;
            width: 168px;
            height: 168px;
            margin: -50px 0 0 -84px;
            border-radius: 100%;
        }
    </style>
</head>
<body>

<div class="layui-card-body" style="text-align: center;">
    <div class="layui-form-item">
        <div style="position: relative;width: 373px;height: 373px;background-color: #F2F2F5;margin: auto;">
            <button type="button" class="layui-btn" id="avatar">
                <i class="layui-icon">&#xe67c;</i>上传头像
            </button>
            <img  src="/static/imghwm/default1.png"  data-src="" id="  class="lazy"   id="avatar_thumb" alt="Encapsulating ThinkPHP6.0 universal file upload" >
            <input type="hidden" name="avatar" value="">
        </div>
    </div>
</div>

<!-- layui.js 和jquery.js 推荐使用本地文件 -->
<script src="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.4/layui.js" charset="utf-8"></script>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="/static/lib/js/common.js"></script>
<script>
    layui.use([&#39;form&#39;, &#39;upload&#39;], function() {
        var upload = layui.upload;

        //图片上传
        common_upload(&#39;avatar&#39;);

        //文件上传
        //common_upload(&#39;avatar&#39;,&#39;file&#39;);
    });
</script>
</body>
</html>

六、在public\static\lib\js目录下创建common.js

/**
 * @desc 通用文件上传
 * @param name  文件存储文件夹
 * @param type 文件类型:默认为图片类型(image)
 */
function common_upload(name,type=&#39;image&#39;) {

    layui.use([&#39;form&#39;, &#39;upload&#39;], function() {
        var upload = layui.upload;

        //选完文件后自动上传
        upload.render({
            elem: &#39;#&#39;+name,
            url: "/upload/upload",
            auto: true,
            accept: &#39;file&#39;, //普通文件
            data:{name:name,type:type},
            
            done: function(data) {
                console.log(data);
                //上传完毕回调
                if (data.code == 0) {
                    return layer.msg(data.msg,{icon:2});
                } else {
                    $("#"+name+"_thumb").attr(&#39;src&#39;,data.result).show();
                    $(&#39;input[name=&#39;+name+&#39;]&#39;).val(data.result);
                }
            }
        });
    });
}

七、测试图片上传

7.1、为了方便,本文就不在本地部署项目了,采用ThinkPHP内置的服务器

进到项目根目录,执行以下命令:

php think run

Encapsulating ThinkPHP6.0 universal file upload

7.2、在浏览器地址栏中输入访问地址,发现报错?

Encapsulating ThinkPHP6.0 universal file upload

7.3、遇到错误不要慌,我们打开ThinkPHP的调试功能,看看具体错误信息

Encapsulating ThinkPHP6.0 universal file upload

7.4、通过开启调试模式,发现报错原因是我们没有安装模板引擎,在ThinkPHP6.0中默认只能支持PHP原生模板,如果需要使用thinkTemplate模板引擎,需要安装think-view扩展

Encapsulating ThinkPHP6.0 universal file upload

7.5、进到项目根目录下,输入以下命令进行安装think-view模板

composer require topthink/think-view

7.6、再次访问,访问成功。不过图片显示的是破裂的,如果觉得不好看,小伙伴们可以自行设置一个默认图片。

Encapsulating ThinkPHP6.0 universal file upload

7.7、测试图片上传。从GIF图中可以看出上传图片大小和后缀不符合配置文件中设置的值,会给出相应的提示信息,只有上传符合配置文件中设置的值的图片才会在页面中显示并存储到本地。

Encapsulating ThinkPHP6.0 universal file upload

7.8、如果需要上传文件,视频或音频,只需要修改以下两个地方,这里就不演示了,小伙伴们下去自己试下。

Encapsulating ThinkPHP6.0 universal file upload

八、新图片上传成功后自动删除原图,有效地减少垃圾信息的累积

8.1、在app\controller\Upload.php中添加delImg() 方法

<?php

namespace app\controller;

use think\exception\ValidateException;

class Upload
{
    //上传
    public function upload()
    {
        //判断是否是POST请求,如果是处理上传逻辑
        if (request()->isPost()){

            //接收文件上传类型
            $type = request()->param(&#39;type&#39;,&#39;&#39;,&#39;trim&#39;);
            $name = request()->param(&#39;name&#39;,&#39;&#39;,&#39;trim&#39;);
            //获取表单上传文件
            $file = request()->file(&#39;file&#39;);
            //组装文件保存目录
            $upload_dir = &#39;/&#39;.$type.&#39;/&#39;.$name;

            try {
                //从config/upload.php配置文件中读取允许上传的文件后缀和大小
                $suffix_config = config(&#39;upload.suffix_arr&#39;);
                $size_config = config(&#39;upload.size_arr&#39;);

                if (empty($size_config[$type]) || empty($size_config[$type])){
                    return false;
                }else{
                    $suffix = $suffix_config[$type];
                    $size = $size_config[$type];
                }
                //验证器验证上传的文件
                validate([&#39;file&#39;=>[
                    //限制文件大小
                    &#39;fileSize&#39;      =>  $size * 1024 * 1024,
                    //限制文件后缀
                    &#39;fileExt&#39;       =>  $suffix
                ]],[
                    &#39;file.fileSize&#39; =>  &#39;上传的文件大小不能超过&#39;.$size.&#39;M&#39;,
                    &#39;file.fileExt&#39;  =>  &#39;请上传后缀为:&#39;.$suffix.&#39;的文件&#39;
                ])->check([&#39;file&#39;=>$file]);

                //上传文件到本地服务器
                $filename = \think\facade\Filesystem::disk(&#39;public&#39;)->putFile($upload_dir, $file);
                if ($filename){
                    $src = &#39;/uploads/&#39;.$filename;
                    return json([&#39;code&#39;=>1,&#39;result&#39;=>$src]);
                }else{
                    return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;上传失败&#39;]);
                }
            }catch (ValidateException $e){
                return json([&#39;code&#39;=>0,&#39;msg&#39;=>$e->getMessage()]);
            }
        }else{
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;非法请求&#39;]);
        }
    }

    //删除旧图片
    public function delImg()
    {
        if (request()->isPost() && request()->isAjax()){
            //获取旧图片地址
            $img_url = input(&#39;img_url&#39;,&#39;&#39;,&#39;trim&#39;);
            //如果旧图片地址为系统默认图片地址直接返回true; 这里的系统默认地址小伙伴可以自行设置
            if($img_url == &#39;/uploads/image/avatar/default/user_avatar.jpg&#39;){
                return true;
            }
            //ROOT_PATH常量建议定义在入口文件index.php或中间件中,这里为了演示方便就先定义在这里。
            define(&#39;ROOT_PATH&#39;,dirname(str_replace("\\",&#39;/&#39;,$_SERVER[&#39;SCRIPT_FILENAME&#39;]))."/");
            //如果接收的图片地址不为空,循环删除
            if (!empty($img_url)){
                $old_image = array(ROOT_PATH.$img_url);
                foreach ($old_image as $img){
                    if (file_exists($img)){
                        @unlink($img);
                    }
                }
            }
            return json([&#39;code&#39;=>1,&#39;msg&#39;=>&#39;图片删除成功&#39;]);
        }else{
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;图片删除失败&#39;]);
        }
    }
}

8.2、在common.js中的common_upload()方法中定义before()

/**
 * @desc 通用文件上传
 * @param name  文件存储文件夹
 * @param type 文件类型:默认为图片类型(image)
 */
function common_upload(name,type=&#39;image&#39;) {

    layui.use([&#39;form&#39;, &#39;upload&#39;], function() {
        var upload = layui.upload;

        //选完文件后自动上传
        upload.render({
            elem: &#39;#&#39;+name,
            url: "/upload/upload",
            auto: true,
            accept: &#39;file&#39;, //普通文件
            data:{name:name,type:type},

            before: function(obj) {
                var img_url = $(&#39;input[name=&#39;+name+&#39;]&#39;).val();
                // 删除老数据
                if (img_url != &#39;&#39;) {
                    $.ajax({
                        url: "/upload/delImg",
                        type: &#39;POST&#39;,
                        data: {
                            img_url: img_url
                        },
                    });
                }
            },

            done: function(data) {
                console.log(data);
                //上传完毕回调
                if (data.code == 0) {
                    return layer.msg(data.msg,{icon:2});
                } else {
                    $("#"+name+"_thumb").attr(&#39;src&#39;,data.result).show();
                    $(&#39;input[name=&#39;+name+&#39;]&#39;).val(data.result);
                }
            }
        });
    });
}

相关推荐:

1. thinkphp技术专题

2. thinkphp视频教程

The above is the detailed content of Encapsulating ThinkPHP6.0 universal file upload. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)