2017-04-21 94 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插件...现在工作正常... –