我有HTML / JavaScript(jQuery)和PHP来上传文件。在我的本地机器上测试时,一切都很完美。当我改变一些东西并将代码推送到服务器时,一切都有效,除了上传冻结率为5%。为什么要这样做?
PHP:
<?php
require_once("includes_sitewide/session.php");
function convert_date($date) {
$s = split('\/', $date);
return $s[2] . '_' . $s[0] . '_' . $s[1];
}
define('UPLOAD_FOLDER', 'uploads');
if (!isset($_SESSION['LOGGED_IN']) || !$_SESSION['LOGGED_IN']) {
http_response_code(401);
echo 'Unauthorized to upload files';
} else {
if (!isset($_FILES['file'])) {
http_response_code(500);
echo 'No file data';
} else if (!isset($_FILES['file']['error']) || is_array($_FILES['file']['error'])) {
http_response_code(500);
echo 'Unknown error';
} else {
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
if (is_writable(UPLOAD_FOLDER)) {
$filename = basename($_FILES['file']['name']);
$target = UPLOAD_FOLDER . '/' . convert_date($_POST['date']) . '_' . time() . '_' . $filename;
if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
if (
file_put_contents($target . '.txt',
"Name(s): " . $_POST['names'] .
"\nTitle: " . $_POST['title'] .
"\nDate: " . $_POST['date'] .
(isset($_POST['notes']) ? "\nNotes:\n" . $_POST['notes'] : ''))
) {
echo $filename . ' uploaded sucessfully';
} else {
http_response_code(500);
echo 'Failed to write notes file';
}
} else {
http_response_code(500);
echo 'Failed to save file as \'' . $target . '\'';
}
} else {
http_response_code(500);
echo 'Target folder \'' . UPLOAD_FOLDER . '\' is not writable';
}
break;
case UPLOAD_ERR_NO_FILE:
http_response_code(500);
echo 'No input file';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
http_response_code(500);
echo 'Exceeded filesize limit!';
break;
default:
http_response_code(500);
echo 'Unknown error!';
}
}
}
?>
的JavaScript(jQuery的):
var fileID = 0;
$('#form').submit(function() {
// validation
var ok = true;
if (!$('input[name=file]').val()) {
appendAlert('danger', 'Please select a file');
ok = false;
}
var names = $('input[name=names]').val();
if (!names) {
appendAlert('danger', 'Please enter name(s)');
ok = false;
}
var title = $('input[name=title]').val();
if (!title) {
appendAlert('danger', 'Please enter announcement title/group');
ok = false;
}
var date = $('input[name=date]').val();
if (!date) {
appendAlert('danger', 'Please enter date');
ok = false;
} else if (!/^[0-1]\d\/[0-3]\d\/\d{4}$/.test(date)) {
appendAlert('danger', 'Date must be in format mm/dd/yyyy');
ok = false;
} else {
var bits = date.split('/');
var d = new Date(bits[2], bits[0] - 1, bits[1]);
if (!d || (d.getMonth() + 1) != bits[0] || d.getDate() != bits[1]) {
appendAlert('danger', 'Date must be valid');
ok = false;
}
}
if (!ok) {
return false;
}
fileID++;
var file = $('input[name=file]').prop('files')[0];
if (file.size > MAX_FILE_SIZE) {
appendAlert('danger', 'File size too large (' + file.size + ' > ' + MAX_FILE_SIZE + ' bytes)');
return false;
}
if (FILE_EXTENSIONS.indexOf(file.name.split('.').pop().toLowerCase()) == -1) {
appendAlert('danger', file.name + ' has an invalid file extension');
return false;
}
var fd = new FormData();
fd.set('file', file);
fd.set('names', names);
fd.set('title', title);
fd.set('date', date);
var notes = $('input[name=notes]').val();
if (notes) {
fd.set('notes', notes);
}
$.ajax({
type: 'POST',
url: 'upload.php',
data: fd,
processData: false,
contentType: false,
success: function(data) {
$('#progress-' + fileID + ' > .progress-bar').css('width', '100%').html('100%').addClass('progress-bar-success');
appendAlert('success', data);
},
error: function(jqXHR, textStatus, errorThrown) {
$('#progress-' + fileID + ' > .progress-bar').css('width', '100%').html('error').addClass('progress-bar-danger');
appendAlert('danger', jqXHR.status + ' ' + errorThrown + ': "' + jqXHR.responseText + '"');
},
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e) {
var x = 0;
if (e.lengthComputable) {
x = Math.floor(e.loaded / e.total * 100);
}
$('#progress-' + fileID + ' > .progress-bar').css('width', x + '%').html(x + "%");
});
return xhr;
}
});
$('#file-table > tbody').append('<tr><td>' + fileID + '</td><td>' + file.name + '</td><td>' + file.size + ' bytes</td><td><div id="progress-' + fileID + '" class="progress" style="margin-bottom:0;"><div class="progress-bar" role="progressbar" style="width: 0;">0%</div></div></td></tr>');
$('#progress').show();
resetForm();
return false;
});