我的网站托管在 godaddy 上并在 php 上运行。我在使用分块上传文件时遇到问题。问题仅在于某些文件。一个这样的文件大小为 5.13 MB。我可以上传 20 MB 的更大文件,甚至更小的文件。问题只出在这个文件上。我检查了内容,内容没有问题。它是纯 .txt 文件。该文件以 6 个块上传,在第 6 个块中它给出 500 内部服务器错误。它能够成功处理分成大约 20 个块(每个块大小为 1 MB)的文件。我在 chrome 和 Edge 中尝试过,同样是问题所在。我正在使用以下 javascript 代码来分块和上传内容。我做了很多谷歌搜索,但我的问题似乎很独特,在任何地方都找不到解决方案。大多数答案都是指与文件大小相关的 500 错误。但正如我所提到的,我能够上传更大的文件。
function sendRequest() {
for(var i=0; i<document.getElementById('file').files.length; i++) {
var blob = document.getElementById('file').files[i];
var BYTES_PER_CHUNK = 1048576;//100 * 1024 * 1024; // 1MB chunk sizes.
//var BYTES_PER_CHUNK = 100 * 1024 * 1024; // 100MB chunk sizes.
var SIZE = blob.size;
var start = 0;
var end = BYTES_PER_CHUNK;
//alert(window["uploadcounter" + i]);
window["uploadcounter" + i]=0;
window["uploadfilearray" + i] = [];
document.getElementById('uploadPercent' + i).innerHTML = "Upload: 0 % ";
while( start < SIZE ) {
var chunk = blob.slice(start, end);
window["uploadfilearray" + i][window["uploadcounter" + i]]=chunk;
window["uploadcounter" + i]=window["uploadcounter" + i]+1;
start = end;
end = start + BYTES_PER_CHUNK;
}
window["uploadcounter" + i]=0;
uploadFile(window["uploadfilearray" + i][window["uploadcounter" + i]], document.getElementById('file').files[i].name, i);
// setInterval(new function() {updateFileProcessingStatus(document.getElementById('file').files[i].name)}, 3000);
}
}
var chunkNumber = 0;
function uploadFile(blobFile, filename, fileIndex) {
alert(blobFile.size + ' chunknumber: ' + ++chunkNumber);
var fd = new FormData();
fd.append("fileToUpload", blobFile);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "./datafileupload.php?filename="+filename + "&chunkNumber=" + chunkNumber + "&r=" + Math.random());
xhr.onload = function(e) {
window["uploadcounter" + fileIndex] = window["uploadcounter" + fileIndex] + 1;
if(window["uploadfilearray" + fileIndex].length > window["uploadcounter" + fileIndex] ){
uploadFile(window["uploadfilearray" + fileIndex][window["uploadcounter" + fileIndex]], filename, fileIndex);
var percentloaded2 = parseInt((window["uploadcounter" + fileIndex]/window["uploadfilearray" + fileIndex].length)*100);
$('#uploadPercent' + fileIndex).html('Upload Percent : '+ filename + ' ' + percentloaded2+' % ');
$('#progressbar' + fileIndex).attr('style', 'width:' + percentloaded2 + '%');
}else{
$('#progressbar' + fileIndex).attr('style', 'width: 100%');
$('#uploadPercent' + fileIndex).html("File uploaded");
processFile(filename);
}
};
xhr.send(fd);
}
同样在另一边,以下是处理上传文件的 php 代码
<?php
ini_set('max_execution_time', 10000);
require("dbconfig.php");
ini_set('auto_detect_line_endings', true);
if ((!isset($_SESSION['newsession']) || $_SESSION['newsession'] == false) && $_SESSION["ROLE"] == 'Admin'){
$name2 = $_GET['filename'];
echo "the chunk received is " . $_GET["chunkNumber"];
$extension = end((explode(".", $name2)));
if($extension != 'csv' || $extension != 'txt') {
exit;
}
$lowerCaseName = strtolower($name2);
$isValidCSV = 0;
if(strpos($lowerCaseName, "gsa") !== false) {
$isValidCSV = 1;
} else if(strpos($lowerCaseName, "product") !== false) {
$isValidCSV = 1;
} else if(strpos($lowerCaseName, "image") !== false) {
$isValidCSV = 1;
} else if(strpos($lowerCaseName, "supplier") !== false) {
$isValidCSV = 1;
} else if(strpos($lowerCaseName, "account") !== false) {
$isValidCSV = 1;
}
if($isValidCSV === 0) {
exit;
}
$name = $_FILES['fileToUpload']['name'];
$target_path = "./uploadedfiles/";
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
$target_file = $target_path.$name;
$complete =$target_path.$name2;
$com = fopen($complete, "ab");
error_log($target_path);
// Open temp file
//$out = fopen($target_file, "wb");
//if ( $out ) {
// Read binary input stream and append it to temp file
$in = fopen($tmp_name, "rb");
if ( $in ) {
while($buff = fread( $in, 1048576 )) {
fwrite($com, $buff);
}
}
fclose($in);
//}
//fclose($out);
fclose($com);
}else{
echo'you are not logged in.';
}
?>