Home > Backend Development > PHP Tutorial > The use of upload class UploadFile using ThinkPHP

The use of upload class UploadFile using ThinkPHP

不言
Release: 2023-03-30 07:58:01
Original
3611 people have browsed it

The UploadFile class in ThinkPHP is used to handle file uploads. This article briefly summarizes the use of the UploadFile class during the learning process.

The use of the upload class is very simple. If you are using the upload function for the first time, you need to pay attention to the fact that the form attributes in the html submission form must be added with the enctype attribute, such as:

Copy code The code is as follows:

 <form method="post" action="upload.php"  enctype="multipart/form-data">
 <input type=&#39;file&#39; name=&#39;file&#39;>
 <input type=&#39;submit&#39; value=&#39;提交&#39;>
 </form>
Copy after login

ThinkPHP’s UploadFile class is in the file ../ThinkPHP/Extend/Library/ORG/Net/UploadFile.class.php:

Copy code The code is as follows:

    //引入UploadFile类
        import(&#39;ORG.Net.UploadFile&#39;);
        //实例化UploadFile类
        $upload  = new UploadFile();
        //设置文件大小
        $upload -> maxSize = 3292200;
        //设置文件保存规则唯一
        $upload->saveRule = &#39;uniqid&#39;;
        //设置上传文件的格式
        $upload -> allowExts = array(&#39;jpg&#39;,&#39;png&#39;,&#39;jpeg&#39;);
        //保存路径
        $upload->savePath =&#39;./Public/Uploads/&#39;;
        //设置需要生成缩略图,仅对图像文件有效
        $upload->thumb = true;
        //设置需要生成缩略图的文件前缀
        $upload->thumbPrefix = &#39;m_&#39;;  //生产缩略图也可以根据需要生成1张或多张,2张:&#39;m_,s_&#39;
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = &#39;150&#39;;//2张的不同设置:&#39;150,200&#39;
        //设置缩略图最大高度
        $upload->thumbMaxHeight = &#39;200&#39;;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        //上传失败返回错误信息
        if(!$upload->upload()){
           $this->error($upload->getErrorMsg());
        }else{
           $this->success(&#39;上传成功&#39;);
           //获取上传文件的信息
           $inf= $upload->getUploadFileInfo();
        }
Copy after login

After setting, you can easily upload files. At the same time, $upload's getUploadFileInfo can obtain the information of the uploaded file.

The above is the detailed content of The use of upload class UploadFile using ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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