Home >Backend Development >PHP Tutorial >Use ThinkPHP+Uploadify to implement image upload function_PHP tutorial
First, unzip the downloaded Uploadify compressed package and place it in the public folder. The implementation code is as follows:
Frontend html part:
<script src="/uploadify/jquery.min.js" data-ke-src="/Public/uploadify/jquery.min.js" type="text/javascript"> <script src="/uploadify/jquery.uploadify.min.js" data-ke-src="/Public/uploadify/jquery.uploadify.min.js" type="text/javascript"> <link rel="stylesheet" type="text/css" href="/uploadify/uploadify.css" data-ke-src="/Public/uploadify/uploadify.css"> <script type="text/javascript"> var img_id_upload=new Array();//初始化数组,存储已经上传的图片名 var i=0;//初始化数组下标 $(function() { $('#file_upload').uploadify({ 'auto' : false,//关闭自动上传 'removeTimeout' : 600,//文件队列上传完成1秒后删除 'swf' : '/Public/uploadify/uploadify.swf', 'uploader' : '/_URL_/Article/uploadify', 'method' : 'post', //方法,服务端可以用$_POST数组获取数据 'buttonText' : '选择图片',//设置按钮文本 'multi' : true,//允许同时上传多张图片 'uploadLimit' : 8,//一次最多只允许上传10张图片 'fileTypeDesc' : 'Image Files',//只允许上传图像 'fileTypeExts' : '*.gif; *.jpg; *.png',//限制允许上传的图片后缀 'fileSizeLimit' : '2000KB',//限制上传的图片大小 'onUploadSuccess' : function(file, data, response) { //每次成功上传后执行的回调函数,从服务端返回数据到前端 $('#image').append('<div style="float:left;margin:2px 0 0 2px"><img width="100px" height="100px" src="/uploads/'+data+'" data-ke-src="/uploads/'+data+'" height=80 width=80 />'); img_id_upload[i]=data; i++; } }); }); </script> <input type="file" name="file_upload" id="file_upload" /> <p><a href="javascript:$('#file_upload').uploadify('upload','');" data-ke-src="javascript:$('#file_upload').uploadify('upload','');">上传</a> </p> <br /> <div id="image" class="image"><br />
action execution code part:
public function uploadify() { $ph=M('Upload'); import('ORG.Net.UploadFile'); $upload = new UploadFile();// 实例化上传类 $upload->maxSize = 93145728 ;// 设置附件上传大小 $upload->saveRule =rand(1,9999); $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg','flv','avi','mov');// 设置附件上传类型 $upload->savePath = './Uploads/';// 设置附件上传目录 if(!$upload->upload()) {// 上传错误提示错误信息 $this->error($upload->getErrorMsg()); }else{// 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); } for($i=0;$i<count($info);$i++) { $data['name']=$info[$i]['savename']; $data['size']=$info[$i]['size']; $data['type']=$info[$i]['extension']; echo $info[$i]['savename']; $rs=$ph->add($data); } /*if($rs) { $this->success("成功"); }else { $this->error("失败"); }*/ }