Home > php教程 > php手册 > ThinkPHP使用心得分享-上传类UploadFile的使用

ThinkPHP使用心得分享-上传类UploadFile的使用

WBOY
Release: 2016-06-13 09:35:05
Original
1154 people have browsed it

上传类的使用很简单,如果是第一次使用上传功能的朋友需要注意的是html提交表单中的表单属性要加上 enctype属性,如:

复制代码 代码如下:


 


 
 
 

ThinkPHP的UploadFile类在文件../ThinkPHP/Extend/Library/ORG/Net/UploadFile.class.php中:

复制代码 代码如下:


        //引入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();
        }

经过设置后就能轻松地上传文件,同时,$upload的getUploadFileInfo可以获取上传的文件的信息。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template