Когда я загружаю файл Excel с помощью php в базу данных (phpMySql), я получаю сообщение об ошибке:
Ошибка загрузки файла "": Не удалось открыть для чтения!Файл не существует
, а в Excel более 40000 строк.Если я загружаю файл Excel с 400-1000 строками, он работает.
Я использую библиотеку PHPExcel.
Вот мой код:
<html>
<head>
<title>Import Excel</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {background-color: #f2f2f2;}
</style>
</head>
<?php
require 'Classes/PHPExcel/IOFactory.php';
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "office";
if(isset($_POST['upload'])){
$inputfilename = $_FILES['file']['tmp_name'];
$exceldata = array();
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection Failed: " . mysqli_connect_error());
}
try {
$inputfiletype = PHPExcel_IOFactory::identify($inputfilename);
$objReader = PHPExcel_IOFactory::createReader($inputfiletype);
$objPHPExcel = $objReader->load($inputfilename);
} catch(Exception $e){
die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for($row = 2; $row <= $highestRow; $row++) {
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$sql = "INSERT INTO allsec (emp_code, emp_full_name, request_type, leave_status, leave_from, leave_to, days, year, month, pl, pm, lpm, status, emp_name, wg, leave_type)
VALUES ('".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."', '".$rowData[0][11]."', '".$rowData[0][12]."', '".$rowData[0][13]."', '".$rowData[0][14]."', '".$rowData[0][15]."')";
if(mysqli_query($conn, $sql)){
$exceldata[] = $rowData[0];
} else{
echo "Error: " .$sql . "<br>" . mysqli_error($conn);
}
}
echo "<table border='1'>";
foreach($exceldata as $index => $excelraw){
echo "<tr>";
foreach($excelraw as $excelcolumn){
echo "<td>".$excelcolumn."</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
}
?>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" >
<input type="submit" name ="upload" value="upload">
</form>
</body>
</html>