PHP 多input file檔案上傳,
前台html
<form id="form" method="post" enctype="multipart/form-data"><input class="imagesUpload" type="file" name="imagesUpload[]" style="width: 152px;"/><input class="imagesUpload" type="file" name="imagesUpload[]" style="width: 152px;"/><input class="imagesUpload" type="file" name="imagesUpload[]" style="width: 152px;"/><input class="imagesUpload" type="file" name="imagesUpload[]" style="width: 152px;"/><a class="easyui-linkbutton" icon="icon-add" id="uploadImage_input">圖片上傳</a></form>
** 注意name的寫法,都要使用name[]方式,並且用同一個name
** 注意帶有file的表單,form一定要加:enctype=”multipart/form-data”
jquery 提交form表單
$("#form").form('submit',{ url:url, success:function(data){ //處理返回資料 }});
PHP 幕後處理
//接收處理檔案$fileArray = $_FILES['imagesUpload'];//根據請求的name擷取檔案$upload_dir = public_path() . "/upload/carPic/";$userID = Session::get('userID');$nowTime = date("YmdHis", time());$i = 0;$successName = array();foreach ($fileArray['error'] as $key => $error){ //遍曆處理檔案 if ( $error == UPLOAD_ERR_OK ) { $temp_name = $fileArray['tmp_name'][$key]; $file_name = $userID.'-'.$nowTime.$i.$fileArray['name'][$key]; move_uploaded_file($temp_name, $upload_dir.$file_name); array_push($successName, $file_name);//把上傳成功的檔案名稱加入數組 }else{ return '{"flag":0,"flagmsg":"上傳[檔案'.$key.']失敗!<br/>!"}'; } $i++;}$flag = array('flag'=>1,'flagmsg'=>'檔案上傳成功!');$names = array('names'=>$successName);return json_encode( array_merge($flag,$names));//返回上傳結果,並返回上傳成功後的所有檔案的名稱
PHP代碼都很簡單,就不一一解釋了。歡迎指導!
尊重原創,轉載請註明出處:http://blog.csdn.net/a437629292/article/details/44337295