PHP MVC框架skymvc支持多文件上传

1.代码upload.ctrl.php    

<?php class uploadControl extends skymvc{ public function __construct(){ parent::__construct(); } public function onDefault(){ $this->smarty->display("upload/default.html"); } public function onUpload(){ $this->loadClass("upload"); //上传的文件目录 $this->upload->uploaddir="attach/my/"; //允许上传的文件大小 $this->upload->maxsize=4194304000; //是否上传图片 $this->upload->upimg=true; //设置允许上传的文件类型 $this->upload->sysallowtype=array('gif','jpg','bmp','png','jpeg','txt','mpeg','avi','rm','rmvb','wmv','flv','mp3','wav','wma','swf','doc','pdf','zip','tar','svg'); $this->upload->allowtype=$this->upload->sysallowtype; //根据Id存储 $this->upload->iddir=0; $data=$this->upload->uploadfile("upimg"); //print_r($data); echo json_encode($data); } } ?>

2.代码upload.html    

<!doctype html> <html> {include file="head.html"} <body> {include file="header.html"} <div> <form method="post" action="/index.php?m=upload&a=upload" enctype="multipart/form-data"> <div> <div> <i></i> 上传文件 <div> <input type="file" multiple> </div> </div> </div> <div></div> <div> <input type="submit" value="上传"> </div> </form> <h3>上传结果</h3> <div></div> </div> {include file="footer.html"} <style> .result{border:1px solid #ccc; padding:5px;} .result div{line-height:24px;} .result .e{color:#D58384;} .result .s{color:#59A42A;} </style> <script src="https://www.jb51.net/static/js/skyupload.js"></script> <script> $(document).on("change","#upimg",function(data){ skyUpload("upimg","/index.php?m=upload&a=upload&ajax=1",function(e){ var data=eval("("+e.target.responseText+")"); if(data.err){ $("#result").append('<div>error:'+data.err+'</div>'); }else{ $("#result").append('<div>success:'+data.filename+'</div>'); } }); }); </script> </body> </html>

3.PHP代码

function skyUpload(upid,url,success,error,uploadProgress) { var vFD = new FormData(); var f= document.getElementById(upid).files; $("#"+upid+"loading").show(); for(var i=0;i<f.length;i++){ vFD.append('upimg', document.getElementById(upid).files[i]); var oXHR = new XMLHttpRequest(); oXHR.addEventListener('load', success, false); oXHR.addEventListener('error', error, false); if(uploadProgress!=undefined){ oXHR.upload.addEventListener("progress", uploadProgress, false); } oXHR.open('POST',url); oXHR.send(vFD); } } /* function uploadFinish(e){ var data=eval("("+e.target.responseText+")"); $("#uploading").hide() if(data.error != '') { skyToast(data.msg); }else { $("#imgShow").html("<img src='/"+data.imgurl+".100x100.jpg'>"); $("#imgurl").val(data.imgurl); } } function uploadError(e) { // upload error skyToast("上传出错了"); } */

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2341097360744283dd449c7993e6af7c.html