TP框架--文件上传


1.在IndexController.class.php里面写2个方法,shangchuan用来显示页面,upload是上传文件的方法。

复制代码
<?php
namespace Home\Controller;
use Think\Controller; class IndexController extends Controller { public function shangchuan() { $this->display(); } public function upload() { $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 31457280 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->autoSub = true; $upload->subName = array('date','Ymd'); $upload->rootPath = "./Public/";//文件上传保存的根路径,下面的Upload文件夹放在这里面,./Public/Upload $upload->savePath = './Uploads/'; // 设置附件上传目录,文件上传上来以后放在了这个文件件里面。 $info = $upload->upload(); if(!$info) // 上传错误提示错误信息  { $this->error($upload->getError()); } else// 上传成功 获取上传文件信息  { foreach($info as $file){ echo $file['savepath'].$file['savename']; } } } }
复制代码

 

2.显示页面

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form action="__CONTROLLER__/upload" method="post" enctype="multipart/form-data"> <input type="file" name="wenjian" /> <input type="submit" value="提交" /> </form> </body> </html>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM