2017-02-28 73 views
0

我花了幾天的時間試圖找出如何使用ajaxForm插件上傳多個文件沒有任何成功。儘管我已經發現了一些關於整體的例子,但它們要麼沒有太大的幫助,要麼顯示單個文件上傳,我已經設法使其工作,但沒有多個文件。ajaxForm提交多個文件上傳

下面是HTML代碼

<div id="upload-wrapper"> 
<div align="center"> 
<form action="driverLoads/fetchDrivers/personnelUploads/processupload.php" method="post" enctype="multipart/form-data" id="uploadForm"> 
    <input name="FileInput[]" id="FileInput" type="file" multiple/> 
    <input type="submit" id="submit-btn" value="Upload" /> 
    <img src="driverLoads/fetchDrivers/personnelUploads/images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/> 
</form> 
<div id="progressbox" ><div id="progressbar"></div ><div id="statustxt">0%</div></div> 
<div id="output"></div> 

和JavaScript代碼:

$(document).ready(function() { 
$('#submit-btn').click(function(e) { 
     e.preventDefault(); 
     $('#uploadForm').ajaxForm({ 
      target: '#output', // target element(s) to be updated with server response 
      beforeSubmit: beforeSubmit, // before submission callback function 
      success:  afterSuccess, // after submission callback 
      uploadProgress: OnProgress, //check on the upload progress 
      resetForm: true,  // reset the form if upload is success 
      data:{driverid:driverid} //send the driverid as well 
     }).submit();    
    }); 

和PHP代碼:

if(isset($_FILES["FileInput"])){ 
    $UploadDirectory = $base_url . '/subSystems/drivers/driverLoads/fetchDrivers/personnelUploads/'; 

for($i = 0; $i < count($_FILES["FileInput"]['name']);$i++){ 
//check if this is an ajax request 
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){ 
     die(); 
    } 


//Is file size is less than allowed size. 
    if($_FILES["FileInput"]["size"][$i] > 5242880) { 
    die("File size is too big!"); 
    } 

//allowed file type Server side check 
switch(strtolower($_FILES['FileInput']['type'][$i])) 
    { 
     //allowed file types 

     //disabled till further notice 
     case 'image/png': 
     case 'image/gif': 
     case 'image/jpeg': 
     case 'image/pjpeg': 
     case 'text/plain': 
     case 'text/html': //html file 

     case 'application/x-zip-compressed': 
     case 'application/pdf': 
     case 'application/msword': 
     case 'application/vnd.ms-excel': 
     case 'video/mp4': 
      break; 
     default: 
      die('Unsupported File!'); //output error 
} 

    if(move_uploaded_file($_FILES['FileInput']['tmp_name'][$i], $UploadDirectory.$_FILES['FileInput']['name'][$i])){ 
     die('Success! File Uploaded.'); 
    }else{ 
     die('Error uploading File!'); 
    } 

    } 

}else{ 
    $error = codeToMessage($_FILES["FileInput"]["error"][$i]); 
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?' . $error); 
} 

我認爲它是所有r要理解的問題的代碼。當我上傳一個文件時,代碼再次正常工作。請注意,如果我上傳的代碼也可以正常工作,比如說爲了爭論3個文件,但是如果只上傳第一個。如果任何人有更多的經驗,請幫忙,因爲我似乎無法發現錯誤。

預先感謝您。

回答

1

在就行了你的PHP代碼,上面寫着:

if(move_uploaded_file($_FILES['FileInput']['tmp_name'][$i], $UploadDirectory.$_FILES['FileInput']['name'][$i])){ 
     die('Success! File Uploaded.'); 

然後嘗試刪除行:

die('Success! File Uploaded.'); 

(或評論吧!)

是否行得通?