Codeigniter 4 upload excel file to database

PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Import Excel and CSV into MySQL help to save the user time and avoid repetitive work.

PHP version 7.2 or newer to develop using PhpSpreadsheet. Other requirements, such as PHP extensions, are enforced by the composer.

  1. Download and install CodeIgniter.
  2. Use Composer to install PhpSpreadsheet into your project:
    composer require phpoffice/phpspreadsheet
  3. Use phpspreadsheet library inside in your controller

The following SQL query creates a user_info table in the MySQL database.

CREATE TABLE `client_info` [
  `id` int[10] NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
  `country_code` varchar[10] COLLATE utf8mb4_unicode_ci NOT NULL,
  `mobile` varchar[100] COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
  `city` varchar[255] COLLATE utf8mb4_unicode_ci NOT NULL,
  `ip_address` varchar[100] COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp[] ON UPDATE current_timestamp[],
  `status` int[10] NOT NULL
] ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Open App/Config/Routes.php file and add following lines.

Create a model file named UserModel.php inside the App/Models folder.

Bài mới nhất

Chủ Đề