2016-06-19 17 views
1

我上傳多個文件與低於此PHP腳本文本:阿賈克斯成功功能無法輸出由PHP腳本發送

<?php 

if(isset($_FILES['uploadfile'])){ 
    $total_files = count($_FILES['uploadfile']['name']); 
    if($total_files > 0){ 

     for($i=0; $i<$total_files; $i++) { 

      $file_name = $_FILES['uploadfile']['name'][$i]; 
      $file_size = $_FILES['uploadfile']['size'][$i]; 
      $file_tmp = $_FILES['uploadfile']['tmp_name'][$i]; 
      $file_type = $_FILES['uploadfile']['type'][$i]; 

      $upload_Path = "storage/".$file_name; 

      //var_dump($file_size); 
      //die; 

      if($file_size > 8000000){ 
       echo ('Total upload size must be less than 8 MB.'); 
       die; 
      } 

      if($file_tmp == ""){ 
       echo ('There is no file path.');  
       die;  
      } 
      else{ 
       if(!file_exists($upload_Path)){ 
        move_uploaded_file($file_tmp, $upload_Path); 
       } 
       else{ 
        $name = pathinfo($file_name, PATHINFO_FILENAME); 
        $ext = pathinfo($file_name, PATHINFO_EXTENSION); 
        $new_name = $name.rand().'.'.$ext; 

        $new_Path = "storage/".$new_name; 
        move_uploaded_file($file_tmp, $new_Path); 
       } 
      } 
     } 
    } 
    die('File uploaded successfully!'); 
} 
?> 

但問題是,每當出現錯誤,如echo ('Total upload size must be less than 8 MB.');它不會使用outputed阿賈克斯。但是,當成功上傳完成後,它會顯示File uploaded successfully!

我的AJAX如下:

$.ajax({ 
       type:'POST', 
       url: 'mupld.php', 
       data: formdata, 
       processData:false, 
       contentType:false, 
       success: function(response){ 
        alert('Success: '+response); 
       }, 
       error: function(xhr, status, error){ 
        alert('Error: '+status+' '+error); 
       } 
      });      

做一個變種轉儲我不明白上面8MB上傳任何輸出,但下面,我得到

Success: <pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>3283515</font> 
</pre> 
+0

什麼是最大文件大小的php.ini設置? – TheDrot

+0

Php.ini設置爲8mb – Ayan

回答

3

@Jeff桶是正確的,所以我編輯了我的答案:

實際上,您應該在成功回調中處理這些錯誤。 error()回調僅用於瀏覽器和服務器之間的連接中斷的情況,而error()參數則希望處理這些情況,例如,典型的textStatus錯誤應該是'未找到'或'內部服務器錯誤',但沒有'總上傳大小必須小於8 MB'。「

您應該返回的信息的數組,你可以在客戶端使用,並處理在成功(),如:

嘗試{

if(isset($_FILES['uploadfile'])){ 
    $total_files = count($_FILES['uploadfile']['name']); 
    if($total_files > 0){ 

     for($i=0; $i<$total_files; $i++) { 

      $file_name = $_FILES['uploadfile']['name'][$i]; 
      $file_size = $_FILES['uploadfile']['size'][$i]; 
      $file_tmp = $_FILES['uploadfile']['tmp_name'][$i]; 
      $file_type = $_FILES['uploadfile']['type'][$i]; 

      $upload_Path = "storage/".$file_name; 

      //var_dump($file_size); 
      //die; 

      if($file_size > 8000000){ 
       echo json_encode(array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') ); 
       die(); 
      } 

      if($file_tmp == ""){ 
       echo json_encode(array('status' => 'failure' , 'msg' => 'There is no filepath.') ); 
       die; 
      } 
      else{ 
       if(!file_exists($upload_Path)){ 
        move_uploaded_file($file_tmp, $upload_Path); 
       } 
       else{ 
        $name = pathinfo($file_name, PATHINFO_FILENAME); 
        $ext = pathinfo($file_name, PATHINFO_EXTENSION); 
        $new_name = $name.rand().'.'.$ext; 

        $new_Path = "storage/".$new_name; 
        move_uploaded_file($file_tmp, $new_Path); 
       } 
      } 
     } 
    } 
    echo json_encode(array('status' => 'success' , 'msg' => 'File uploaded succesfully.') ); 
    die(); 
} 

else{ 

    echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files")); 
    die(); 
} 


} 
catch(Exception $ex){ 

echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage())); 
die(); 
} 
finally{ 
    die(); 
} 

然後在$阿賈克斯()函數:

$("#uploadfile").change(function(){ 
//submit the form here 
var files = $("#fileupload")[0]; 
var formdata = new FormData(files); 
$.ajax({ 
    type:'POST', 
    url: 'mupld.php', 
    data: formdata, 
    processData:false, 
    contentType:false, 
    success: function(response){ 

      response = JSON.parse(response); 
      alert(response.msg); 

     }, 
    error: function(xhr, textStatus, error){ 
     console.log('Error: '+textStatus+' '+error); 
    } 
}); 

如果你特別想在錯誤()回調處理這個問題,你應該在PHP腳本的響應代碼爲500 - 或任何自定義代碼 - 用頭()。

+0

好的。我會試一試。 – Ayan

+0

我相信'die()'仍然會發送一個HTTP 200響應,這將不會觸發Ajax錯誤' –

+0

@Jeff Puckett II,如果它不起作用,你會有什麼建議? – Ayan