我就直接进入正题了。 我正在尝试使用 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(以 mb 为单位的 kb)乘以 1024(以 kb 为单位的 b)得到 524,288,000 (二)
确保您的 post_max_size 大于 upload_file_size,并且您的 memory_limit 大于 post_max_size(默认内存限制为 128MB)
希望这有帮助。
关于php - 上传文件 - 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532161/