上传文件 - 500内部服务器错误

时间:2017-04-21 00:54:44

标签: php jquery ajax codeigniter

我会直截了当地说到这一点。 我正在尝试使用jquery和codeigniter上传400mb + zip文件。 但是,当进度条完成时,它会在控制台日志上显示 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;

    }
}

提前感谢。

2 个答案:

答案 0 :(得分:2)

你应该检查的第一件事是上传到文件夹的权限如果它没有读/写访问权限(例如775),那么你将得到500错误。

如果最初不起作用,我建议您清除浏览器cookie并缓存,重新加载并重试。您仍然应该纠正500000 / 512000k错误,但这是一个容易(并且常见的)错误。在这种情况下,你乘以500 * 1024(kb in a mb)然后乘以1024(b in a kb)得到524,288,000 (b)中

确保你的post_max_size大于upload_file_size并且你的memory_limit大于post_max_size(默认内存限制是128MB)

希望这有帮助。

答案 1 :(得分:0)

我假设您正在执行AJAX请求。如果是这样,并且如果您使用的是Chrome,请不要检查控制台,而是检查网络标签。在那里,它应该显示最后一个请求,包括标题,响应,输出和所有这些。检查一下,告诉我们你先看到了什么。这是调试AJAX的正确方法。