PHP ช่วยด้วยจ้า import csv file : Fatal error: Maximum execution time of 30 seconds exceeded
รบกวนท่านผู้รู้ ช่วยมาตอบหน่อยค่ะ พอดีเขียน Import csv file เพื่อนำข้อมุลเข้า DB
แต่พบ error
Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\import_csv\index.php on line 24
ซึ่งไปหาอ่านๆมา คือให้ไปแก้ ที่ php.ini
เปลี่ยน max_execution_time = 360 (หรือมากกว่าก็ได้) แต่ก็ยังไม่หายค่ะ
ข้อมูล โดยปกติข้อมูลจะมีประมาณ 9000+ rows 39 Col.
แต่ลองตัดให้เหลือแค่ 1000 rows และ 2 Col. ข้อมูลเข้าประมาณ 700-800 rows และติด error นี้ค่ะ
Code (PHP)
<?php
$conn = mysqli_connect("localhost", "root", "12345678","DB001");
date_default_timezone_set("Asia/Bangkok");
$trn_date = date("Y-m-d H:i:s");
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {
$sqlInsert = "INSERT INTO users (id, Name)
values ('" . $column[0] . "','" . $column[1] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min.js"></script>
<style>
body {
font-family: Arial;
width: 1000;
}
.outer-scontainer {
background: #F0F0F0;
border: #e0dfdf 1px solid;
padding: 20px;
border-radius: 2px;
}
.input-row {
margin-top: 0px;
margin-bottom: 20px;
}
.btn-submit {
background: #333;
border: #1d1d1d 1px solid;
color: #f0f0f0;
font-size: 0.9em;
width: 100px;
border-radius: 2px;
cursor: pointer;
}
.outer-scontainer table {
border-collapse: collapse;
width: 100%;
}
.outer-scontainer th {
border: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
.outer-scontainer td {
border: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
#response {
padding: 10px;
margin-bottom: 10px;
border-radius: 2px;
display:none;
}
.success {
background: #c7efd9;
border: #bbe2cd 1px solid;
}
.error {
background: #fbcfcf;
border: #f3c6c7 1px solid;
}
div#response.display-block {
display: block;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#frmCSVImport").on("submit", function () {
$("#response").attr("class", "");
$("#response").html("");
var fileType = ".csv";
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + fileType + ")$");
if (!regex.test($("#file").val().toLowerCase())) {
$("#response").addClass("error");
$("#response").addClass("display-block");
$("#response").html("Invalid File. Upload : <b>" + fileType + "</b> Files.");
return false;
}
return true;
});
});
</script>
</head>
<body>
<h2>Import CSV file.</h2>
<div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
<div class="outer-scontainer">
<div class="row">
<form class="form-horizontal" action="" method="post"
name="frmCSVImport" id="frmCSVImport" enctype="multipart/form-data">
<div class="input-row">
<label class="col-md-4 control-label">Choose CSV
File</label> <input type="file" name="file"
id="file" accept=".csv">
<button type="submit" id="submit" name="import"
class="btn-submit">Import</button>
<br />
</div>
</form>
</div>
<?php
$sqlSelect = "SELECT * FROM users";
$result = mysqli_query($conn, $sqlSelect);
if (mysqli_num_rows($result) > 0) {
?>
<table id='userTable'>
<thead>
<tr>
<th>id</th>
<th>Name</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Name']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } ?>
</div>
</body>
</html>
Tag : PHP, MySQL, Appserv
Date :
2019-05-09 11:49:47
By :
Pornwichian
View :
1009
Reply :
7
แก้ไข 2 บรรทัดนี้ด้วยครับ ใน php.ini
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
Date :
2019-05-09 12:03:18
By :
Manussawin
แก้ไข ตามคำแนะนำจากทั้งสองท่าน ยังขึ้นเหมือนเดิมเลยค่ะ
Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\import_csv\index.php on line 24
พอมีคำแนะนำให้ตรวจสอบตรงจุดอื่นอีกไหมคะ
ประวัติการแก้ไข 2019-05-09 13:58:58
Date :
2019-05-09 13:51:31
By :
Pornwichian
เอาโค๊ดที่แก้ไขแล้วมาลงอีกทีครับ
Date :
2019-05-09 14:06:36
By :
Chaidhanan
รบกวนหน่อยนะคะ ขอบคุณค่ะ
Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\import_csv\index3.php on line 22
Code (PHP)
<?php
$conn = mysqli_connect("localhost", "root", "12345678","DB001");
date_default_timezone_set("Asia/Bangkok");
$trn_date = date("Y-m-d H:i:s");
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
/*while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {*/
while ($column = fgetcsv($file, 1000, ",")) {
$sqlInsert = "INSERT INTO users (id, Name)
values ('" . $column[0] . "','" . $column[1] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min.js"></script>
<style>
body {
font-family: Arial;
width: 1000;
}
.outer-scontainer {
background: #F0F0F0;
border: #e0dfdf 1px solid;
padding: 20px;
border-radius: 2px;
}
.input-row {
margin-top: 0px;
margin-bottom: 20px;
}
.btn-submit {
background: #333;
border: #1d1d1d 1px solid;
color: #f0f0f0;
font-size: 0.9em;
width: 100px;
border-radius: 2px;
cursor: pointer;
}
.outer-scontainer table {
border-collapse: collapse;
width: 100%;
}
.outer-scontainer th {
border: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
.outer-scontainer td {
border: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
#response {
padding: 10px;
margin-bottom: 10px;
border-radius: 2px;
display:none;
}
.success {
background: #c7efd9;
border: #bbe2cd 1px solid;
}
.error {
background: #fbcfcf;
border: #f3c6c7 1px solid;
}
div#response.display-block {
display: block;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("#frmCSVImport").on("submit", function () {
$("#response").attr("class", "");
$("#response").html("");
var fileType = ".csv";
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + fileType + ")$");
if (!regex.test($("#file").val().toLowerCase())) {
$("#response").addClass("error");
$("#response").addClass("display-block");
$("#response").html("Invalid File. Upload : <b>" + fileType + "</b> Files.");
return false;
}
return true;
});
});
</script>
</head>
<body>
<h2>Import CSV file.</h2>
<div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
<div class="outer-scontainer">
<div class="row">
<form class="form-horizontal" action="" method="post"
name="frmCSVImport" id="frmCSVImport" enctype="multipart/form-data">
<div class="input-row">
<label class="col-md-4 control-label">Choose CSV
File</label> <input type="file" name="file"
id="file" accept=".csv">
<button type="submit" id="submit" name="import"
class="btn-submit">Import</button>
<br />
</div>
</form>
</div>
<?php
$sqlSelect = "SELECT * FROM users";
$result = mysqli_query($conn, $sqlSelect);
if (mysqli_num_rows($result) > 0) {
?>
<table id='userTable'>
<thead>
<tr>
<th>id</th>
<th>Name</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Name']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } ?>
</div>
</body>
</html>
Date :
2019-05-09 14:14:08
By :
Pornwichian
ขอบคุณทั้งสองท่านที่มาช่วยตอบนะคะ ตอนนี้น้องทำได้แล้วนะคะ
เพิ่ม set_time_limit(0); ไปในไฟล์ที่ import ค่ะ
ตอนนี้ทดสอบ 9000 rows up ก็สามารถ import เข้าได้ค่ะ
ประวัติการแก้ไข 2019-05-10 16:31:08
Date :
2019-05-09 15:12:30
By :
Pornwichian
Date :
2019-05-10 13:02:04
By :
Manussawin
Date :
2019-05-10 13:25:51
By :
nobetaking
Load balance : Server 04