rashiqulrony/laravel-csv-import

Import Very Large CSV Without File Store and Chunk Upload system Laravel package to export and import CSV files in a memory-optimized way

v1.0.0 2025-07-01 12:12 UTC

This package is auto-updated.

Last update: 2025-07-02 06:06:20 UTC


README

License: MIT

A Laravel package to import large CSV files efficiently using chunked memory processing, on-the-fly UTF-8 conversion, and no temporary storage overhead.

๐Ÿ“ฆ Installation

Install via Composer:

composer require rashiqulrony/laravel-csv-import

If you're using Laravel <5.5 or your Laravel version doesn't support package auto-discovery, add the service provider manually:

// config/app.php
'providers' => [
    Rashiqulrony\CSVImport\Providers\AppServiceProvider::class,
];

โš™๏ธ Configuration

Publish the config file (optional):
php artisan vendor:publish --provider="Rashiqulrony\CSVImport\Providers\AppServiceProvider" --tag=config

This creates a file: config/csvimport.php

return [
    'chunk_size' => 200, // Number of rows to return per batch
];

๐Ÿš€ Usage

โœ… Basic Example

use Rashiqulrony\CSVImport;

public function upload(Request $request)
{
    $request->validate([
        'file' => 'required|file|mimes:csv',
    ]);

    $result = CSVImport::upload($request->file('file'));
    
    if (!empty($result) && is_array($result)) {
         foreach ($result as $key => $value) {
              foreach ($result[$key] as $data) {
                 // Process or save the row (e.g. DB::table(...)->insert($row))
              }
         }
    } else {
         return back()->with('error', 'Data not found or invalid file.');
    }
        
    return back()->with('success', 'CSV imported successfully!');
}

๐Ÿ“ฅ Method: CSVImport::upload($file)

Returns Description
array On success: returns a chunk of rows (associative arrays).
['status' => false, 'message' => '...'] On failure.

โœ… Features

  • Detects and converts multiple encodings to UTF-8
  • Skips invalid rows (missing fields, malformed headers)
  • Efficient memory usage with chunking
  • No need to manually store or manage temp files

๐Ÿงช Supported Encodings

  • UTF-8
  • UTF-16LE / UTF-16BE
  • Windows-1252
  • ISO-8859-1

๐Ÿ“‚ Directory Structure

laravel-csv-import/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ CSVImport.php
โ”‚   โ””โ”€โ”€ Providers/
โ”‚       โ””โ”€โ”€ AppServiceProvider.php
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ csvimport.php
โ”œโ”€โ”€ composer.json
โ””โ”€โ”€ README.md

๐Ÿ“ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ‘ค Author

Rashiqul Rony
๐Ÿ“ง rashiqulrony@gmail.com
๐Ÿ”— github.com/rashiqulrony