1

I'm going straight to the point here. I'm trying to upload a 400mb+ zip file using jquery and codeigniter. however, when the progress bar completes it gives me 500 Internal Server Error on the console log don't know what's causing this. I've tried it on my local files everything works fine. but when I put it online it gives me this 500 internal server error.

my hosting and my local have the same settings already.

upload_max_filesize 500M

post_max_size 500M

max_execution_time 3000

Here's my code:

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 CODE

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;

    }
}

thanks in advance.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56

2 Answers2

4

The first thing you should check is the permissions on the upload-to folder If it doesn't have read/write access (775 for example), then you'll get a 500 error.

If that doesn't work initially, I suggest you clear your browser cookies and cache, reload and try again. You should still rectify the 500000/512000k error however, it's an easy (and commonly-made ) mistake. In this instance you multiply 500 * 1024 ( kb in a mb) then by 1024 (b in a kb) to get 524,288,000 (b)

Ensure your post_max_size is greater than your upload_file_size and that your memory_limit is greater than the post_max_size (the default memory limit is 128MB)

Hope this helps.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • hi rachel, folder permission I am sure it is okay since I can upload file around 38mb.. I also tried clearing cache and cookies but the problem still persists. by the way, what do you mean about the `$config['max_size']` which is equal to 50000? what is the difference? – Sam Teng Wong Apr 21 '17 at 02:43
  • The difference is 12000 b! You're calculating the value incorrectly. There are 1024 bytes in a kilobyte, not 1000. So you should multiply x by 1024, in this case 500 x 1024, to get the max file size – Rachel Gallen Apr 21 '17 at 02:47
  • I see, so it should be 512000? will this cause the 500 internal server error also? – Sam Teng Wong Apr 21 '17 at 02:48
  • It couldn't help! If the max you can upload at the moment is 38 mb it sounds like the max is configured to 40000 in error. Definitely worth looking at! – Rachel Gallen Apr 21 '17 at 02:51
  • Cool.. Best of luck – Rachel Gallen Apr 21 '17 at 02:52
  • Hi Rachelle, I have tried it and also I tried to put zero on it.. so that there will be no limit... still shows me 500 internal server error.. – Sam Teng Wong Apr 21 '17 at 03:09
  • Did you clear browser cache and cookies after you had made these corrections? – Rachel Gallen Apr 21 '17 at 03:10
2

I assume you're performing an AJAX request. If so, and if you're using Chrome, do not check console, but the Network tab. There, it should show you the last request made, with the headers, the response, the output and all that. Check there and tell us what you see first. That's the proper way to debug AJAX.