簡體   English   中英

上傳文件-500內部服務器錯誤

[英]Upload File - 500 Internal Server Error

我直接說到這里。 我正在嘗試使用jquery和codeigniter上傳400mb +的zip文件。 但是,進度條完成后,控制台日志上顯示500 Internal Server Error ,這不知道是什么原因引起的。 我已經在本地文件上嘗試過了,一切正常。 但是當我將其放到網上時,會出現此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;

    }
}

提前致謝。

您應該檢查的第一件事是上載到文件夾的權限如果它沒有讀/寫訪問權限(例如775),則將出現500錯誤。

如果最初無法解決問題,建議您清除瀏覽器Cookie並緩存,重新加載並重試。 您仍然應該糾正500000 / 512000k錯誤,但這是一個簡單的(通常是常見的)錯誤。 在這種情況下,您將500 * 1024(以kb為單位)乘以1024(以b為單位)得到524,288,000(b)

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

希望這可以幫助。

我假設您正在執行AJAX請求。 如果是這樣,並且如果您使用的是Chrome,則不要檢查控制台,而要檢查“網絡”標簽。 在那里,它應該顯示最后一個請求,包括標題,響應,輸出以及所有內容。 檢查那里,並告訴我們您首先看到的內容。 這是調試AJAX的正確方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM