我一直在学习有关创建文件上传的教程,即使total_line
在另一个php文件中声明,index.php中的“未解决的变量total_line”也会出现错误:然后在[ C0]。
下面的代码是我在其中获取total_line变量以及在其中创建上传文件网页的文件。
data.total_line
if (isset($_POST['hidden_field'])) {
$error = '';
$total_line = '';
session_start();
if ($_FILES['file']['name'] != '') {
$allowed_extension = array('csv');
$file_array = explode(".", $_FILES["file"]["name"]);
$extension = end($file_array);
if (in_array($extension, $allowed_extension)) {
$new_file_name = rand().
'.'.$extension;
$_SESSION['csv_file_name'] = $new_file_name;
move_uploaded_file($_FILES['file']['tmp_name'], 'file/'.$new_file_name);
$file_content = file('file/'.$new_file_name, FILE_SKIP_EMPTY_LINES);
$total_line = count($file_content);
} else {
$error = 'Only CSV file format is allowed';
}
} else {
$error = 'Please Select File';
}
if ($error != '') {
$output = array(
'error' => $error
);
} else {
$output = array(
'success' => true,
'total_line' => ($total_line - 1)
);
}
echo json_encode($output);
}
文件的执行结果为<!DOCTYPE html>
<html>
<head>
<title>Import CSV File Data</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<br />
<div class="container">
<h1 align="center">Import CSV File Data with Progress Bar </h1>
<br />
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Import CSV File Data</h3>
</div>
<div class="panel-body">
<span id="message"></span>
<form id="sample_form" method="POST" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label class="col-md-4 control-label">Select CSV File</label>
<input type="file" name="file" id="file" />
</div>
<div class="form-group" align="center">
<input type="hidden" name="hidden_field" value="1" />
<input type="submit" name="import" id="import" class="btn btn-info" value="Import" />
</div>
</form>
<div class="form-group" id="process" style="display:none;">
<div class="progress">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<span id="process_data">0</span> - <span id="total_data">0</span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function() {
var clear_timer;
$('#sample_form').on('submit', function(event) {
$('#message').html('');
event.preventDefault();
$.ajax({
url: "upload.php",
method: "POST",
data: new FormData(this),
dataType: "json",
contentType: false,
cache: false,
processData: false,
beforeSend: function() {
$('#import').attr('disabled', 'disabled');
$('#import').val('Importing');
},
success: function(data) {
if (data.success) {
$('#total_data').text(data.total_line);
start_import();
clear_timer = setInterval(get_import_data, 2000);
$('#message').html('<div class="alert alert-success">CSV File Uploaded</div>');
}
if (data.error) {
$('#message').html('<div class="alert alert-danger">' + data.error + '</div>');
$('#import').attr('disabled', false);
$('#import').val('Import');
}
}
})
});
function start_import() {
$('#process').css('display', 'block');
$.ajax({
url: "import.php",
success: function() {
}
})
}
function get_import_data() {
$.ajax({
url: "process.php",
success: function(data) {
var total_data = $('#total_data').text();
var width = Math.round((data / total_data) * 100);
$('#process_data').text(data);
$('.progress-bar').css('width', width + '%');
if (width >= 100) {
clearInterval(clear_timer);
$('#process').css('display', 'none');
$('#file').val('');
$('#message').html('<div class="alert alert-success">Data Successfully Imported</div>');
$('#import').attr('disabled', false);
$('#import').val('Import');
}
}
})
}
});
</script>
,并且什么也没有发生。