JQ插件ajaxFileUpload、php实现图片,数据同时上传

时间:2022-12-05 04:53:27

代码结构如下:

JQ插件ajaxFileUpload、php实现图片,数据同时上传

1、HTML代码,没必要解释了。

 <!DOCTYPE html>
 <html>
     <head>
         <meta charset="UTF-8">
         <title>文件上传</title>
         <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
         <script src="uploads/ajaxFileUpload.js" type="text/javascript" charset="utf-8"></script>
         <script type="text/javascript">
             $(function(){
                 $('#ti').click(function(){
                     var data = { name: 'my name', description: 'short description' }
                     $.ajaxFileUpload({
                         url: 'up.php',
                         secureuri: false,
                         data: data,
                         fileElementId: 'upf',
                         dataType: 'json',
                         success: function (data) {
                            // var datejson=eval(data);
                              //console.log(data[0].path_name)
                          $('#im').append('<img src="'+data[0].path_name+'">')
                              //console.log('<img src="'+data[0].path_name+'">')
                         },
                         error: function (data) {
                               console.log(data)
                         }
                     });

                 })

             })
         </script>
     </head>
     <body>
     <!--<form action="up.php" method="post" enctype="multipart/form-data">
     </form>-->
         <input  type="file" name="upfm" id="upf" value="" />
         <input id='ti' type="button" value="提交"/>

         <div id="im">

         </div>

     </body>

 </html>

2、关于ajaxFileUpload插件,在下面代码中如果你使用的是JQ1.9以上请复制1-12到你的ajaxFileUpload代码中,JQ在很早就废弃了handleError方法。注释关于代码的解释很清楚。

 jQuery.extend({
     handleError: function( s, xhr, status, e )         {

                 if ( s.error ) {
                     s.error.call( s.context || s, xhr, status, e );
                 }

                 if ( s.global ) {
                     (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
                 }
    },
     createUploadIframe: function (id, uri) {//id为当前系统时间字符串,uri是外部传入的json对象的一个参数
         //create frame
         var frameId = 'jUploadFrame' + id; //给iframe添加一个独一无二的id
         var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"'; //创建iframe元素
         if (window.ActiveXObject) {//判断浏览器是否支持ActiveX控件
             if (typeof uri == 'boolean') {
                 iframeHtml += ' src="' + 'javascript:false' + '"';
             }            else if (typeof uri == 'string') {
                 iframeHtml += ' src="' + uri + '"';
             }
         }
         iframeHtml += ' />';
         jQuery(iframeHtml).appendTo(document.body); //将动态iframe追加到body中
         return jQuery('#' + frameId).get(0); //返回iframe对象
     },
     createUploadForm: function (id, fileElementId, data) {//id为当前系统时间字符串,fileElementId为页面<input type='file' />的id,data的值需要根据传入json的键来决定
         //create form
         var formId = 'jUploadForm' + id; //给form添加一个独一无二的id
         var fileId = 'jUploadFile' + id; //给<input type='file' />添加一个独一无二的id
         var form = jQuery('<form  action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data" ></form>'); //创建form元素
         if (data) {//通常为false
             for (var i in data) {
                 jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form); //根据data的内容,创建隐藏域,这部分我还不知道是什么时候用到。估计是传入json的时候,如果默认传一些参数的话要用到。
             }
         }        var oldElement = jQuery('#' + fileElementId); //得到页面中的<input type='file' />对象
         var newElement = jQuery(oldElement).clone(); //克隆页面中的<input type='file' />对象
         jQuery(oldElement).attr('id', fileId); //修改原对象的id
         jQuery(oldElement).before(newElement); //在原对象前插入克隆对象
         jQuery(oldElement).appendTo(form); //把原对象插入到动态form的结尾处
         //set attributes
         jQuery(form).css('position', 'absolute'); //给动态form添加样式,使其浮动起来,
         jQuery(form).css('top', '-1200px');
         jQuery(form).css('left', '-1200px');
         jQuery(form).appendTo('body'); //把动态form插入到body中
         return form;
     },
     ajaxFileUpload: function (s) {//这里s是个json对象,传入一些ajax的参数
         // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
         s = jQuery.extend({}, jQuery.ajaxSettings, s); //此时的s对象是由jQuery.ajaxSettings和原s对象扩展后的对象
         var id = new Date().getTime(); //取当前系统时间,目的是得到一个独一无二的数字
         var form = jQuery.createUploadForm(id, s.fileElementId, (typeof (s.data) == 'undefined' ? false : s.data)); //创建动态form
         var io = jQuery.createUploadIframe(id, s.secureuri); //创建动态iframe
         var frameId = 'jUploadFrame' + id; //动态iframe的id
         var formId = 'jUploadForm' + id; //动态form的id
         // Watch for a new set of requests
         if (s.global && !jQuery.active++) {//当jQuery开始一个ajax请求时发生
             jQuery.event.trigger("ajaxStart"); //触发ajaxStart方法
         }        var requestDone = false; //请求完成标志
         // Create the request object
         var xml = {};        if (s.global)
             jQuery.event.trigger("ajaxSend", [xml, s]); //触发ajaxSend方法
         // Wait for a response to come back
         var uploadCallback = function (isTimeout) {//回调函数
             var io = document.getElementById(frameId); //得到iframe对象
             try {                if (io.contentWindow) {//动态iframe所在窗口对象是否存在
                     xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null;
                     xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document;
                 } else if (io.contentDocument) {//动态iframe的文档对象是否存在
                     xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null;
                     xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document;
                 }
             } catch (e) {
                 jQuery.handleError(s, xml, null, e);
             }            if (xml || isTimeout == "timeout") {//xml变量被赋值或者isTimeout == "timeout"都表示请求发出,并且有响应
                 requestDone = true; //请求完成
                 var status;                try {
                     status = isTimeout != "timeout" ? "success" : "error"; //如果不是“超时”,表示请求成功
                     // Make sure that the request was successful or notmodified
                     if (status != "error") {                        // process the data (runs the xml through httpData regardless of callback)
                         var data = jQuery.uploadHttpData(xml, s.dataType); //根据传送的type类型,返回json对象,此时返回的data就是后台操作后的返回结果
                         // If a local callback was specified, fire it and pass it the data
                         if (s.success)
                             s.success(data, status); //执行上传成功的操作
                         // Fire the global callback
                         if (s.global)
                             jQuery.event.trigger("ajaxSuccess", [xml, s]);
                     } else
                         jQuery.handleError(s, xml, status);
                 } catch (e) {
                     status = "error";
                     jQuery.handleError(s, xml, status, e);
                 }                // The request was completed
                 if (s.global)
                     jQuery.event.trigger("ajaxComplete", [xml, s]);                // Handle the global AJAX counter
                 if (s.global && ! --jQuery.active)
                     jQuery.event.trigger("ajaxStop");                // Process result
                 if (s.complete)
                     s.complete(xml, status);
                 jQuery(io).unbind();//移除iframe的事件处理程序
                 setTimeout(function () {//设置超时时间
                     try {
                         jQuery(io).remove();//移除动态iframe
                         jQuery(form).remove();//移除动态form
                     } catch (e) {
                         jQuery.handleError(s, xml, null, e);
                     }
                 }, 100)
                 xml = null
             }
         }        // Timeout checker
         if (s.timeout > 0) {//超时检测
             setTimeout(function () {                // Check to see if the request is still happening
                 if (!requestDone) uploadCallback("timeout");//如果请求仍未完成,就发送超时信号
             }, s.timeout);
         }        try {            var form = jQuery('#' + formId);
             jQuery(form).attr('action', s.url);//传入的ajax页面导向url
             jQuery(form).attr('method', 'POST');//设置提交表单方式
             jQuery(form).attr('target', frameId);//返回的目标iframe,就是创建的动态iframe
             if (form.encoding) {//选择编码方式
                 jQuery(form).attr('encoding', 'multipart/form-data');
             }            else {
                 jQuery(form).attr('enctype', 'multipart/form-data');
             }
             jQuery(form).submit();//提交form表单
         } catch (e) {
             jQuery.handleError(s, xml, null, e);
         }
         jQuery('#' + frameId).load(uploadCallback); //ajax 请求从服务器加载数据,同时传入回调函数
         return { abort: function () { } };
     },
     uploadHttpData: function (r, type) {        var data = !type;
         data = type == "xml" || data ? r.responseXML : r.responseText;        // If the type is "script", eval it in global context
         if (type == "script")
             jQuery.globalEval(data);        // Get the JavaScript object, if JSON is used.
         if (type == "json")
             eval("data = " + data);        // evaluate scripts within html
         if (type == "html")
             jQuery("<div>").html(data).evalScripts();        return data;
     }
 })

3。php代码

 <?php
     //print_r($_FILES);
 //echo json_encode(print_r($_FILES));

 /**
  * 生成唯一字符串
  * @return string
  */
 function getUniName(){
     return md5(uniqid(microtime(true),true));
 }

 /**
  * 得到文件的扩展名
  * @param string $filename
  * @return string
  */
 function getExt($filename){
     return strtolower(end(explode(".",$filename)));
 }

 /**
  * 构建上传文件信息
  * @return array
  */

 function buildInfo(){
     if(!$_FILES){
         return ;
     }
     $i=0;
     foreach($_FILES as $v){
         //单文件
         if(is_string($v['name'])){
             $files[$i]=$v;
             $i++;
         }else{
             //多文件
             foreach($v['name'] as $key=>$val){
                 $files[$i]['name']=$val;
                 $files[$i]['size']=$v['size'][$key];
                 $files[$i]['tmp_name']=$v['tmp_name'][$key];
                 $files[$i]['error']=$v['error'][$key];
                 $files[$i]['type']=$v['type'][$key];
                 $i++;
             }
         }
     }
     return $files;
 }
 function uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true){
     if(!file_exists($path)){//判断是否有$path文件夹,没有则创建
         mkdir($path,0777,true);//0777表示最大权限
     }
     $i=0;
     $files=buildInfo();
     if(!($files&&is_array($files))){
         return ;
     }
     foreach($files as $file){
         if($file['error']===UPLOAD_ERR_OK){//就是0
             $ext=getExt($file['name']);
             //检测文件的扩展名
             if(!in_array($ext,$allowExt)){
                 exit("非法文件类型");
             }
             //校验是否是一个真正的图片类型
             if($imgFlag){
                 if(!getimagesize($file['tmp_name'])){
                     exit("不是真正的图片类型");

                 }else{
                     $file["filesize"]=getimagesize($file['tmp_name']);
                     //把文件信息付给$file 传到前台返回时数组
                     //如 [720, 1280, 2, "width="720" height="1280"", 8, 3, "image/jpeg"]
                 }
             }
             //上传文件的大小
             if($file['size']>$maxSize){
                 exit("上传文件过大");
             }
             if(!is_uploaded_file($file['tmp_name'])){
                 exit("不是通过HTTP POST方式上传上来的");
             }
             $filename=getUniName().".".$ext;//改文件重新命名
             $destination=$path."/".$filename;
             if(move_uploaded_file($file['tmp_name'], $destination)){
                 $file['name']=$filename;
                 $file['path_name']=$destination;
                 unset($file['tmp_name'],$file['size'],$file['type']);//去除不需要传给的信息
                 $uploadedFiles[$i]=$file;
                 $i++;
             }
         }else{
             switch($file['error']){
                     case 1:
                         $mes="超过了配置文件上传文件的大小";//UPLOAD_ERR_INI_SIZE
                         break;
                     case 2:
                         $mes="超过了表单设置上传文件的大小";            //UPLOAD_ERR_FORM_SIZE
                         break;
                     case 3:
                         $mes="文件部分被上传";//UPLOAD_ERR_PARTIAL
                         break;
                     case 4:
                         $mes="没有文件被上传1111";//UPLOAD_ERR_NO_FILE
                         break;
                     case 6:
                         $mes="没有找到临时目录";//UPLOAD_ERR_NO_TMP_DIR
                         break;
                     case 7:
                         $mes="文件不可写";//UPLOAD_ERR_CANT_WRITE;
                         break;
                     case 8:
                         $mes="由于PHP的扩展程序中断了文件上传";//UPLOAD_ERR_EXTENSION
                         break;
                 }
                 echo $mes;
             }
     }
     return $uploadedFiles;
 };

 $rows=uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true);

 echo json_encode($rows);

uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true);

这段php代码可以更改的可以上传其他文件和大小限制。getimagesize是判断是否为病毒文件更改后缀。

上述代码直接复制即可完成图片和用户数据的同时上传。

返回结果:[{"name":"d032a4ee7e957d956c8af0039d7e3085.jpg","error":0,"filesiz":{"0":720,"1":1280,"2":2,"3":"width=\"720\" height=\"1280\"","bits":8,"channels":3,"mime":"image\/jpeg"},"path_name":"uploads\/d032a4ee7e957d956c8af0039d7e3085.jpg"}]