The use of upload class UploadFile using ThinkPHP

不言
Release: 2023-03-30 07:58:01
Original
3398 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:

 
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('ORG.Net.UploadFile');
        //实例化UploadFile类
        $upload  = new UploadFile();
        //设置文件大小
        $upload -> maxSize = 3292200;
        //设置文件保存规则唯一
        $upload->saveRule = 'uniqid';
        //设置上传文件的格式
        $upload -> allowExts = array('jpg','png','jpeg');
        //保存路径
        $upload->savePath ='./Public/Uploads/';
        //设置需要生成缩略图,仅对图像文件有效
        $upload->thumb = true;
        //设置需要生成缩略图的文件前缀
        $upload->thumbPrefix = 'm_';  //生产缩略图也可以根据需要生成1张或多张,2张:'m_,s_'
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '150';//2张的不同设置:'150,200'
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '200';
        //删除原图
        $upload->thumbRemoveOrigin = true;
        //上传失败返回错误信息
        if(!$upload->upload()){
           $this->error($upload->getErrorMsg());
        }else{
           $this->success('上传成功');
           //获取上传文件的信息
           $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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!