php ajax异步文件上传
1,html表彰与jquery代码
<html> <head> <meta charset="utf-8"> <title>异步上传文件 - www.jb200.com</title> <link href="ajaxfileupload.css" type="text/css" rel="stylesheet"> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="ajaxfileupload.js"></script> <script type="text/javascript"> function ajaxFileUpload() { var file_id = 'fileToUpload'; $("#loading") .ajaxStart(function(){ $(this).show(); }) .ajaxComplete(function(){ $(this).hide(); }); $.ajaxFileUpload ( { url:'doajaxfileupload.php?file_id='+file_id, secureuri:false, fileElementId:file_id, dataType: 'json', data:{name:'logan', id:'id'}, success: function (data, status) { if(typeof(data.error) != 'undefined') { if(data.error != '') { alert(data.error); }else { alert(data.msg); } } }, error: function (data, status, e) { alert(e); } } ) return false; } </script> </head> <body> <div id="wrapper"> <div id="content"> <h1>ajax异步上传演示代码</h1> <img id="loading" src="loading.gif" style="display:none;"> <form name="form" action="" method="POST" enctype="multipart/form-data"> <table cellpadding="0" cellspacing="0" class="tableForm"> <thead> <tr> <th>Please select a file and click Upload button</th> </tr> </thead> <tbody> <tr> <td><input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input"></td> </tr> </tbody> <tfoot> <tr> <td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td> </tr> </tfoot> </table> </form> </div> </body> </html>
2、doajaxfileupload.php
<?php $error = ""; $msg = ""; $fileElementName = $_GET['file_id']; //上传文件类型列表 $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png', 'application/octet-stream', 'application/zip', 'application/x-zip-compressed' ); if(!emptyempty($_FILES[$fileElementName]['error'])) { switch($_FILES[$fileElementName]['error']) { case '1': $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; break; case '2': $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break; case '3': $error = 'The uploaded file was only partially uploaded'; break; case '4': $error = 'No file was uploaded.'; break; case '6': $error = 'Missing a temporary folder'; break; case '7': $error = 'Failed to write file to disk'; break; case '8': $error = 'File upload stopped by extension'; break; case '999': default: $error = 'No error code avaiable'; } }elseif(emptyempty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none') { $error = '没有找到文件..'; }else { //获取上传文件的后缀名 $filetype = pathinfo($_FILES[$fileElementName]["name"], PATHINFO_EXTENSION); if(!in_array($_FILES[$fileElementName]["type"], $uptypes) && $filetype != 'rar') //文件类型过滤 //检查文件类型 { $error = '文件类型不符合('.$_FILES[$fileElementName]["type"].')'; } else{ //重命名后缀名 $upName = time().'.'.$filetype; $msg .= $upName; // move_uploaded_file($_FILES[$fileElementName]['tmp_name'],"./upload/" . $upName); } } echo "{"; echo "error: '" . $error . "',n"; echo "msg: '" . $msg . "'n"; echo "}"; ?>