tp框架文件上传
1、首先我们要在Controller文件夹下新建一个控制器TestController.class.php
1 <?php 2 namespace Home\Controller; 3 use Think\Controller; 4 class TestController extends Controller{ 5 public function shangchuan (){ 6 $this->show(); 7 } 8 public function wjsc(){ 9 $u = new\Think\Upload(); 10 $u->maxSize = 1024000;//设置文件大小 11 $u->rootPath = "./Public/";//文件存放的根路径 12 $u->savePath = "./upload/";//设置当前上传文件存放位置 13 $u->exts = array('jpg','gif','png','jpeg'); 14 $info = $u->upload();//上传文件并返回文件信息 15 if($info){ 16 echo "上传成功,文件存放在:".$info["file"]['savepath']; 17 } 18 else{ 19 echo $u->getError(); 20 } 21 } 22 }
2、在View文件夹下新建一个Test文件夹,再在里边新建一个shangchuan.html页面
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <h1>文件上传</h1> 10 <form action="__CONTROLLER__/wjsc" method="post" enctype="multipart/form-data"> 11 <input type="file" name="file" /> 12 <input type="submit" value="上传" /> 13 </form> 14 </body> 15 </html>
3.效果图