获取上传类
ThinkPHP的扩展中提供了文件上传类库UploadFile,可以在在http://www.thinkphp.cn/extend/224.html下载,或者下载官方的完整扩展包(http://www.thinkphp.cn/down/253.html)里面也已经包含上传扩展类了。如果是单独下载的上传类库,把解压后的UploadFile.class.php 放入ThinkPHP/Extend/Library/ORG/Net/(如果没有请手动创建)目录下面。
效果:
前台代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>上传</title> 5 </head> 6 <body> 7 <form action="__URL__/upload/" id="upload" method=‘post‘ enctype="multipart/form-data" > 8 <input name="image" type="file" /> 9 <input type="submit" value="提交" > 10 </form> 11 </body> 12 </html>
后台代码:
1 <?php 2 class UploadAction extends Action{ 3 /** 4 * 入口文件 5 */ 6 public function index(){ 7 $this->display(); 8 } 9 10 /** 11 * 上传文件 12 */ 13 public function upload(){ 14 15 //引入上传文件类 16 import(‘ORG.Net.UploadFile‘); 17 18 //实例化上传类 19 $upload = new UploadFile(); 20 21 22 //配置上传参数 23 //基础配置 24 //上传文件大小 25 $upload->maxSize = 3145728; 26 //允许上传文件后缀 27 $uplaod->allowExts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘bmp‘, ‘jepg‘); 28 //保存路径 29 $upload->savePath = ‘./Uploads/‘; 30 31 //配置上传缩略图 32 //是否开启上传 33 $upload->thumb = true; 34 //缩略图最大宽度,多个使用逗号隔开 35 $upload->thumbMaxWidth = ‘81.5‘; 36 //缩略图最大高度,多个使用逗号隔开 37 $upload->thumbMaxHeight = ‘133‘; 38 //缩略图前缀 39 $upload->thumbPrefix = ‘thumb_‘; 40 //缩略图保存路径 41 $upload->thumbPath = ‘./Uploads/Thumb/‘; 42 43 44 45 //上传文件 46 if ( !$upload->upload() ) { 47 echo ‘上传失败:‘ . $upload->getErrorMsg(); 48 } else { 49 echo ‘上传成功!<br/>‘; 50 print_r($upload->getUploadFileInfo()); 51 } 52 } 53 }
时间: 2024-10-04 07:45:50