Home >Backend Development >PHP Tutorial >The use of upload class UploadFile using ThinkPHP
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='file' name='file'> <input type='submit' value='提交'> </form>
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(); }
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!