大家好,我要上传我的Excel到sql,但是当我上传时出现此错误,但有镜头屏幕,你看到它了,谢谢
这是index.php,我不知道它在哪个区域有问题,请帮助我,让我遇到这个问题,谢谢 一切正常,但仅收到该错误,同时也无法上传到sql。
<?php
$connect = mysqli_connect("localhost", "root", "", "invoice4");
$output = '';
if(isset($_POST["import"]))
{
$extension = end(explode(".", $_FILES["excel"]["name"])); //
For getting Extension of selected file
$allowed_extension = array("xls", "xlsx", "csv"); //allowed
extension
if(in_array($extension, $allowed_extension)) //check selected
file extension is present in allowed extension array
{
$file = $_FILES["excel"]["tmp_name"]; // getting temporary
source of excel file
include("PHPExcel/IOFactory.php"); // Add PHPExcel Library
in this code
$objPHPExcel = PHPExcel_IOFactory::load($file); // create
object of PHPExcel library by using load() method and in
load method define path of selected file
$output .= "<label class='text-success'>Data Added: </label>
<br /><table class='table table-bordered'>";
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
for($row=2; $row<=$highestRow; $row++)
{
$name = mysqli_real_escape_string($connect, $worksheet-
>getCellByColumnAndRow(0, $row)->getValue());
$email = mysqli_real_escape_string($connect, $worksheet-
>getCellByColumnAndRow(1, $row)->getValue());
if(!empty($name) || !empty($email)) // if none of the data
are empty
{
$output .= "<tr>";
$query = "INSERT INTO tbl_excel(name,email) VALUES
('".$name."', '".$email."')";
mysqli_query($connect, $query);
$output .= '<td>'.$name.'</td>';
$output .= '<td>'.$email.'</td>';
$output .= '</tr>';
}
}
}
$output .= '</table>';
$target_dir = "uploads/"; //file upload folder
$target_file = $target_dir .time().basename($_FILES["excel"]["name"]); //
target file to be uploaded
//upload the file
if (move_uploaded_file($_FILES["excel"]["tmp_name"], $target_file)) {
$fileUploadMsg= "<label class='text-success'>The file has been
uploaded Successfully!</label><br>";
} else {
$fileUploadMsg= '<label class="text-danger">Sorry, there was an
error uploading your file!</label><br>';
}
}
else
{
$output = '<label class="text-danger">Invalid File</label>';
//if non excel file then
}
}
?>
<html>
<head>
<title>Import Data to SQL</title>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet" />
<style>
body
{
margin:0;
padding:0;
background-color: #0cb313b3;
}
.box
{
width:700px;
border:1px solid #ccc;
background-color:#fff;
border-radius:5px;
margin-top:100px;
}
input[type="file"]{
border:1px solid gray;
}
</style>
</head>
<body>
<div class="container box">
<form method="post" enctype="multipart/form-data">
<div class="container-fluid">
<h3 align="center" class="text-success" style="font-
weight:600;">Excel to Mysql Importer</h3><br />
<div class="row" style="margin-bottom:20px">
<div class="col-md-4 col-xs-4 col-sm-4"></div> <!-- Blank
Div -->
<div class="col-md-4 ">
<img src="img/excel.png" height="150px" width="150px">
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<label>Select Excel File*</label>
</div>
<div class="col-xs-6 col-md-5 col-sm-6 col-lg-5">
<input type="file" name="excel" />
</div>
<div class="col-xs-7 col-md-7 col-sm-6 col-lg-7">
<input type="submit" name="import" class="btn btn-info"
value="Import" style="padding:2px 20px;"/>
</div>
</div>
</div>
</form>
<br />
<br />
<?php
echo $output;
echo @$fileUploadMsg;
echo "<hr/>
<p style='float:left'>* Supported Formats: .xls | .xlsx | .csv</p>
<p style='float:right'><a href='export.php'>Exporter →</a>
</p>";
mysqli_close($connect);
?>
</div>
</body>
</html>
答案 0 :(得分:0)
您是否正在使用旧版本的PHP?尝试更改此内容:
$extension = end(explode(".", $_FILES["excel"]["name"]));
对此:
$excel_name = explode(".", $_FILES["excel"]["name"]);
$extension = end($excel_name);
因为documentation提到end()
函数仅接受数组变量(而不是函数)作为参数。