Home > PHP Framework > ThinkPHP > tp file upload

tp file upload

花姐姐
Release: 2020-05-06 13:50:41
forward
2585 people have browsed it

tp file upload

After using ThinkPHP3 frequently, I spent a lot of effort on using ThinkPHP5. Today I will summarize the file upload of tp5. The specific code details are as follows:

Template page (certain To add enctype="multipart/form-data"):

 <form action="{:url(&#39;index/index/upQuestionsWrite&#39;)}" method="post" class="form form-horizontal" enctype="multipart/form-data" id="addform">    
        <div class="row cl">  
        <label class="form-label col-xs-4 col-sm-2">选择试题文件:</label>  
        <div class="btn-upload form-group">  
            <input type="text" name="uploadfile" id="uploadfile" class="input-text upload-url radius" readonly><a href="javascript:void();" class="btn btn-primary radius"><i class="Hui-iconfont"></i>浏览文件</a>  
            <input type="file" name="examfile" class="input-file" multiple>                 
        </div>      
        <boutton type="submit" class="btn btn-success btn-submit">导入试题</button>  
        </div>      
    </form>
Copy after login

Controller (the file name for getting uploaded files in tp5 is slightly different from tp3):

public function upQuestionsWrite()  
    {  
        // 获取表单上传文件  
        $file = request()->file(&#39;examfile&#39;);  
        if(empty($file)) {  
            $this->error(&#39;请选择上传文件&#39;);  
        }  
        // 移动到框架应用根目录/public/uploads/ 目录下  
        $info = $file->move(ROOT_PATH.&#39;public&#39;.DS.&#39;upload&#39;); 
        //如果不清楚文件上传的具体键名,可以直接打印$info来查看  
        //获取文件(文件名),$info->getFilename()  ***********不同之处,笔记笔记哦
        //获取文件(日期/文件名),$info->getSaveName()  **********不同之处,笔记笔记哦
        $filename = $info->getSaveName();  //在测试的时候也可以直接打印文件名称来查看 
        if($filename){              
            $this->success(&#39;文件上传成功!&#39;);  
        }else{  
            // 上传失败获取错误信息  
            $this->error($file->getError());  
        }  
    }     
            // 上传失败获取错误信息    
            $this->error($file->getError());    
        }    
    }
Copy after login

Recommended learning: TP5

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

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template