<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <title>测试页面</title> <link rel="stylesheet" href="__PUBLIC__/Uploadify/uploadify.css"> <js file='__PUBLIC__/Uploadify/jquery-1.9.1.min.js'/> <js file='__PUBLIC__/Uploadify/jquery.uploadify.min.js'/> </head> <body> <p>1.自带上传类</p> <div class="file_holder"> <form action="__URL__/upload" enctype="multipart/form-data" method="post" > <input type="file" name="photo" /> <input type="submit" value="提交" > </form> </div> <br> <p>2.uploadify插件</p> <div> <img id="img" src="http://www.thinkphp.cn/Public/new/img/header_logo.png" width="130" height="130" border="0" /> <input id="file_upload" name="file_upload" type="file" multiple="true" value="" /> </div> <p>图片的输出</p> <div> <volist name="res" id="vo"> <img src="__ROOT__/Uploads/{$vo.goods_img}" alt="" heigh="130" width="130"> </volist> </div> </body> <script type="text/javascript"> //上传插件 $(function() { $('#file_upload').uploadify({ 'swf' : '__PUBLIC__/uploadify/uploadify.swf',//进度动画 'uploader' : '{:U("Index/uploadify")}', 'width' : 120, //上传按钮宽度 'height' : 30, //上传按钮高度 //'buttonText' : '上传头像',//提示 'buttonImage' : '__PUBLIC__/Uploadify/browse-btn.png', //上传按钮背景图地址 经测试火狐未安装flash图片显示错误 'fileTypeDesc' : 'Image File', //选择文件提示文字 'fileTypeExts' : '*.jpeg; *.jpg; *.png; *.gif', //允许选择的文件类型 'onUploadSuccess' : function(file, data, response) { $('#img').attr('src','__ROOT__/Uploads/'+ data); }, }); }); </script> </html>
控制器代码 <?php /** * 日 期:2017-1-13 * 版 本:1.0.0 * 功能说明:图片上传控制器演示。 * **/ namespace Home\Controller; use Think\Controller; class IndexController extends Controller{ public function index(){ $this->display(); } //自带上传类 public function upload(){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg','pdf');// 设置附件上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->upload(); //print_r($info); if(!$info) {// 上传错误提示错误信息 $this->error($upload->getError()); }else{// 上传成功 $this->success('上传成功!'); foreach($info as $file){ $bigimg = $file['savepath'].$file['savename']; } } $model = M('img'); // 取得成功上传的文件信息 $info = $upload->upload(); // 保存当前数据对象 $data['goods_img'] = $bigimg; $model->add($data); } //插件图像上传 public function uploadify(){ if (!empty($_FILES)) { //图片上传设置 $config = array( 'maxSize' => 3145728, 'savePath' => '', 'saveName' => array('uniqid',''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date','Ymd'), ); $upload = new \Think\Upload($config);// 实例化上传类 $images = $upload->upload(); //判断是否有图 if($images){ $info=$images['Filedata']['savepath'].$images['Filedata']['savename']; //返回文件地址和名给JS作回调用 echo $info; }else{ $this->error($upload->getError());//获取失败信息 } } $model = M('img'); // 保存当前数据对象 $data['goods_img'] = $info; $model->add($data); } ?>