一. 在控制器中写一个方法,用于上传
public function upload(){ if (!empty($_FILES)) { //图片上传设置 $config = array( ‘maxSize‘ => 3145728, ‘rootPath‘ => ‘Public‘, ‘savePath‘ => ‘/Uploads/‘, ‘saveName‘ => array(‘uniqid‘,‘‘), ‘exts‘ => array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘), ‘autoSub‘ => false, ‘subName‘ => array(‘date‘,‘Ymd‘), ); $upload = new \Think\Upload($config);// 实例化上传类 $images = $upload->upload(); //判断是否有图 if($images){ $info=$images[‘Filedata‘][‘savename‘]; //返回文件地址和名给JS作回调用 echo $info; } else{ $this->error($upload->getError());//获取失败信息 } } }
二.模板
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Index</title> <link rel="stylesheet" href="__PUBLIC__/uploadify.css"> <script src=‘__PUBLIC__/jquery.js‘></script> <script src=‘__PUBLIC__/jquery.uploadify.min.js‘></script> </head> <body> <div id="imgs"><img width="200px" src="__PUBLIC__/uploads/1.jpg"></div> <input id="file_upload" name="file_upload" type="file" multiple="true" value="" /> </body> <script> var img = ‘‘; $(‘#file_upload‘).uploadify({ ‘swf‘ : ‘__PUBLIC__/uploadify.swf‘, ‘uploader‘ : ‘{:U("Index/upload")}‘, //上传的方法 ‘buttonText‘ : ‘缩略图上传‘, ‘onUploadSuccess‘ : function(file, data, response) { //把所有上传的图片都放入DIV中 img += "<img width=‘200px‘ src=‘__PUBLIC__/Uploads/"+data+"‘>"; $(‘#imgs‘).html(img); } }); </script> </html>
OK,完成。
时间: 2024-10-25 17:48:48