File: /var/www/html/barcode/admin/includes/import-product.php
<?php
session_start();
include('../../includes/config.php');
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(isset($_POST['save_excel_data']))
{
$fileName = $_FILES['import_file']['name'];
$file_ext = pathinfo($fileName, PATHINFO_EXTENSION);
$allowed_ext = ['xls','csv','xlsx'];
if(in_array($file_ext, $allowed_ext))
{
$inputFileNamePath = $_FILES['import_file']['tmp_name'];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileNamePath);
$data = $spreadsheet->getActiveSheet()->toArray();
$count = "0";
foreach($data as $row)
{
if($count > 0)
{
$product_name = $row['0'];
$product_code = $row['1'];
$thickness = $row['2'];
$size = $row['3'];
$batch_no = $row['4'];
$slab_no = $row['5'];
$book_matching = $row['6'];
$grade = $row['7'];
$barcode = $row['8'];
$insertProduct = "INSERT INTO barcodes (`product_name`, `product_code`, `thickness`, `size`, `batch_no`, `slab_no`, `book_matching`, `grade`, `barcode`) VALUES ('$product_name','$product_code','$thickness','$size','$batch_no','$slab_no','$book_matching','$grade','$barcode')";
$result = mysqli_query($con, $insertProduct);
$msg = true;
}
else
{
$count = "1";
}
}
if(isset($msg))
{
$_SESSION['message'] = "Successfully Imported";
header('Location: ../manage-barcode.php');
exit(0);
}
else
{
$_SESSION['message'] = "Not Imported";
header('Location: ../manage-barcode.php');
exit(0);
}
}
else
{
$_SESSION['message'] = "Invalid File";
header('Location: ../manage-barcode.php');
exit(0);
}
}