PHP AjaxFileUpload.js文件上传

Posted 伟洪winnie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP AjaxFileUpload.js文件上传相关的知识,希望对你有一定的参考价值。

html

<script type="text/javascript" src="common/js/ajaxfileupload.js"></script>
<div class="form-group"> <label>Images</label> <br/> <input id="fileToUpload" name="fileToUpload" class="form-control" onchange="ajaxFileUpload('fileToUpload', 'inputFileAgent');" accept="image/jpeg,image/png" type="file" style="display:none" size="45"> <div style="overflow: hidden"> <input class="btn btn-default" style="float: left" type="button" onclick="document.getElementById('fileToUpload').click()" value="Browse..." /> <div id="inputFileAgent" style="float: left; padding: 6px 12px;"></div> <!--<button class="btn btn-default" id="buttonUpload" onclick="return ajaxFileUpload('fileToUpload');" style="float: left">Upload</button>--> </div> <br/> <img id="picupload"/> <input id="images" name="images" type="hidden"></div>
<script>
function ajaxFileUpload($name){ $.ajaxFileUpload ( { url:'doajaxfileupload.php', secureuri:false, fileElementId: $name, dataType: 'json', data:{imageurl:"materials/info/", name:$name }, success: function (data, status){ if(typeof(data.error) != 'undefined') { if(data.error != '') { alert(data.error); }else { $("#inputFileAgent").html(data.oldname); $("#picupload").attr('src','./thumbs/150x100/1/materials/info/'+data.msg); $("#images").val(data.msg); } } }, error: function (data, status, e){ alert(e); } } ) return false; }</script>

doajaxfileupload.php

<?phprequire_once dirname(__FILE__) . '/includes/main.inc.php';
$error = ""; $msg = ""; $name = $_POST['name']; $fileElementName = $name;
if(!empty($_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(empty($_FILES[$name]['tmp_name']) || $_FILES[$name]['tmp_name'] == 'none') { $error = 'No file was uploaded..'; }elseif(! in_array($_FILES[$name]['type'], array("image/jpeg", "image/png"))){ $error = 'File type error'; }elseif($_FILES[$name]['size'] > 1024*1024*10){ $error = 'File size more than 10M'; }else { $ext = extend($_FILES[$name]['name']); $oldname = $_FILES[$name]['name'];
$path = $_POST['imageurl']; if (!file_exists($path)) { mkdir($path, 0777); } $image_name = time().rand(100,999).".".$ext; $tmp = $_FILES[$name]['tmp_name']; move_uploaded_file($tmp, $path.$image_name);
$msg = $image_name;// $msg .= " File Name: " . $_FILES[$name]['name'] . ", ";// $msg .= " File Size: " . @filesize($_FILES[$name]['tmp_name']); //for security reason, we force to remove all uploaded file @unlink($_FILES[$name]); } echo "{"; echo "error: '" . $error . "',\n"; echo "msg: '" . $msg . "',\n"; echo "oldname: '" . $oldname . "'\n"; echo "}";
function extend($file_name){ $extend = pathinfo($file_name); $extend = strtolower($extend["extension"]); return $extend;}?>


以上是关于PHP AjaxFileUpload.js文件上传的主要内容,如果未能解决你的问题,请参考以下文章

利用ajaxfileupload.js异步上传文件

ajaxFileUpload.js插件支持多文件上传的方法

ajaxFileUpload上传带参数文件及JS验证文件大小

ajaxfileupload.js上传文件时后台用java怎么接收文件流

Jquery+ajaxfileupload上传文件

使用ajaxfileupload.js实现上传文件功能