48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
class Home extends BaseController
|
|
{
|
|
public function index(): string
|
|
{
|
|
// return view('welcome_message');
|
|
return view('upload');
|
|
}
|
|
|
|
public function upload_1()
|
|
{
|
|
if ($_FILES['file']['name']) {
|
|
$allowed_extension = ['xls', 'xlsx'];
|
|
$file_array = explode(".", $_FILES['file']['name']);
|
|
$file_extension = end($file_array);
|
|
if (in_array($file_extension, $allowed_extension)) {
|
|
$file_name = $_FILES['file']['tmp_name'];
|
|
$spreadsheet = IOFactory::load($file_name);
|
|
$data = $spreadsheet->getActiveSheet()->toArray();
|
|
// echo "<pre>";
|
|
// print_r($data);die;
|
|
|
|
foreach ($data as $row) {
|
|
$name = $row[0];
|
|
$email = $row[1];
|
|
$age = $row[2];
|
|
|
|
$sql = "INSERT INTO users (name, email, age) VALUES ('$name', '$email', '$age')";
|
|
|
|
if ($conn->query($sql) === TRUE) {
|
|
echo "New record created successfully<br>";
|
|
} else {
|
|
echo "Error: " . $sql . "<br>" . $conn->error . "<br>";
|
|
}
|
|
}
|
|
} else {
|
|
echo 'Only .xls or .xlsx file allowed';
|
|
}
|
|
} else {
|
|
echo 'Please select a file';
|
|
}
|
|
}
|
|
}
|