2017-04-21 93 views
0

我正在直接指向這裏。 我想用jquery和codeigniter上傳一個400mb + zip文件。但是,當進度條完成時,它會給我提供500內部服務器錯誤在控制檯日誌中不知道是什麼導致了這種情況。我已經在我的本地文件上試過了,一切正常。但是當我把它放在網上它給了我這500內部服務器錯誤。上傳文件 - 500內部服務器錯誤

我的託管和我的本地已經有相同的設置。

upload_max_filesize 500M

post_max_size 500M

max_execution_time 3000

這裏是我的代碼:

HTML

<h1>Upload File</h1> 
<hr /> 
<form method="post" name="upload_file" data-base-url="<?php echo site_url(array("main", "upload")); ?>" id="upload_file" action="<?php echo site_url(array("main", "do_upload")); ?>" enctype="multipart/form-data"> 
    <p>File Type: <strong>(*)All</strong></p> 
    <!-- <p>File Type: <strong>doc, docx, pdf, xls, xlsx</strong> and <strong>txt</strong>.</p> --> 
    <input type="file" name="myfile" class="form-control" required><br> 
    <input type="submit" name="cmd_file_upload" id="cmd_file_upload" value="Upload File to Server" class="btn btn-default"> 
</form> 
<br /> 

<p>File Uploaded: <a href="<?php echo base_url(); ?>uploaded_files/<?php echo $result['new_filename']; ?>" target="_blank"><?php echo $result['original_filename']; ?></a></p> 
<div class="progress" style="display: none;"> 
    <div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%"> 
     0% Complete (success) 
    </div> 
</div> 

JQUERY

$("#upload_file").on("submit", function(e){ 
     e.preventDefault(); 

     $("#cmd_file_upload").attr("disabled","disabled"); 
     $(".progress").hide().show(); 
     var $this = $(this); 
     var $url_transaction = $this.attr('action'); 
     var $base_url = $this.data('base-url'); 
     var formData = new FormData($this[0]); 
     $.ajax({ 
      xhr: function() { 
       var xhr = new window.XMLHttpRequest(); 

       xhr.upload.addEventListener("progress", function(evt) { 
       if (evt.lengthComputable) { 
        var percentComplete = evt.loaded/evt.total; 
        percentComplete = parseInt(percentComplete * 100); 
        console.log(percentComplete); 
        $(".progress-bar").attr('style','width:'+percentComplete+'%'); 
        $(".progress-bar").html(percentComplete+'%'); 
        if (percentComplete === 100) { 
        $(".progress-bar").html(percentComplete+'% Complete (Success)'); 
        } 

       } 
       }, false); 

       return xhr; 
      }, 
      beforeSend:function(){ 
       $(".progress-bar").attr('style','width:0%'); 
       $(".progress-bar").html(); 
      }, 
      url: $url_transaction, 
      type: "POST", 
      data: formData, 
      contentType: false, 
      processData: false, 
      // dataType: "json", 
      success: function(result) { 
       console.log(result); 

       setTimeout(function(){ 
       if(result == 0){ 
        window.location.href = $base_url; 
       }else{ 
        window.location.href = $base_url+"/"+result; 
       } 
       }, 500); 

      } 
     }); 
     }); 

PHP代碼

public function do_upload(){ 

    $filename = "file_".md5(date('Y-m-d H:i:s')); 
    $config['file_name']  = $filename; 
    $config['upload_path']   = './uploaded_files'; 
    $config['allowed_types']  = '*'; 
    // $config['allowed_types']  = 'doc|docx|pdf|xls|xlsx|txt'; 
    $config['max_size']    = 500000; 

    $this->load->library('upload', $config); 

    if (! $this->upload->do_upload('myfile')) 
    { 
      $error = array('error' => $this->upload->display_errors('<span>','</span>')); 
      $err = array("status_id" => "0", "message" => $error['error']); 
      $_SESSION['type'] = "warning"; 
      $_SESSION['message'] = $error['error']; 
      echo 0; 
    } 
    else 
    { 
      $data = array('upload_data' => $this->upload->data()); 
      $prev_filename=$data['upload_data']['client_name']; 
      $file_ext = $this->upload->data("file_ext"); 
      $new_filename = $filename.$file_ext; 

      $result = $this->main_m->insert_data('uploaded_file', array('original_filename' => $prev_filename, 'new_filename' => $new_filename)); 

      $_SESSION['type'] = "success"; 
      $_SESSION['message'] = "File's Successfully Uploaded!"; 
      echo $result; 

    } 
} 

在此先感謝。

+0

你有沒有試過htaccess:php_value upload_max_filesize 500M? – Vickel

+0

嗨@Vickel ...是這個設置已經在我的服務器的PHP配置... –

+0

檢查你的系統日誌和PHP錯誤日誌 – Nutscracker

回答

2

您應該檢查的第一件事是上傳到文件夾的權限如果它沒有讀取/寫入權限(例如775),那麼您將得到500錯誤。

如果這不起作用,我建議您清除瀏覽器cookie和緩存,重新加載並重試。您仍然應該糾正500000/512000k錯誤,但這是一個容易(並且常見)的錯誤。在這種情況下,你乘以500 * 1024(kb在一個MB),然後乘以1024(B在一個kb)獲得524,288,000 (b)

確保您的post_max_size大於upload_file_size,並且您的memory_limit大於post_max_size(默認的內存限制是128MB)

希望這會有所幫助。

+0

嗨rachel,文件夾權限我相信這是可以的,因爲我可以上傳38mb左右的文件..我也嘗試清除緩存和餅乾,但問題仍然存在。順便說一句,你是什麼意思關於'$ config ['max_size']'等於50000?有什麼不同? –

+0

區別是12000 b!你正在計算錯誤的值。千字節中有1024個字節,而不是1000個。因此,您應該乘以1024 x 1024,在這種情況下爲500 x 1024,以獲得我看到的最大文件大小 –

+0

,所以它應該是512000?這會導致500內部服務器錯誤嗎? –

0

我假設你正在執行AJAX請求。如果是這樣,並且您正在使用Chrome,請不要檢查控制檯,而是選擇網絡標籤。在那裏,它應該向你展示最後的請求,包括標題,響應,輸出和所有這些。在那裏檢查並告訴我們你先看到什麼。這是調試AJAX的正確方法。

+0

嗨,謝謝你..我已經嘗試使用plupload插件...現在工作正常... –