csv files in php

How to manipulate (CRUD operations) CSV files in PHP?

0 / 0 2 Mins Admin

January 21, 2023

10 Comments

Manipulating CSV or Excel files in PHP is easy with the built-in functions fgetcsv() and fputcsv() for reading and writing CSV files, and PhpSpreadsheet library for reading and writing Excel files.

fgetcsv() are useful to read a CSV file line by line, while fputcsv() method helps to write data to a CSV file.

Here are examples of basic PHP methods that perform CRUD operations on a CSV file:

// Create a new CSV file
function createCSV($data, $fileName) {
    $file = fopen($fileName, 'w');
    foreach ($data as $line) {
        fputcsv($file, $line);
    }
    fclose($file);
}

// Read from a CSV file
function readCSV($fileName) {
    $file = fopen($fileName, 'r');
    $data = array();
    while (($line = fgetcsv($file)) !== FALSE) {
        $data[] = $line;
    }
    fclose($file);
    return $data;
}

// Update a CSV file
function updateCSV($data, $fileName) {
    $file = fopen($fileName, 'w');
    foreach ($data as $line) {
        fputcsv($file, $line);
    }
    fclose($file);
}

// Delete a CSV file
function deleteCSV($fileName) {
    unlink($fileName);
}

The PhpSpreadsheet library provides a set of classes. These classes are helpful to read and write several spreadsheet file formats including Excel and LibreOffice.

Here is an example of CRUD operations for CSV files using the PhpSpreadsheet library:

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// CREATE
// Add data to the sheet
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Email');
$sheet->setCellValue('A2', '1');
$sheet->setCellValue('B2', 'John Doe');
$sheet->setCellValue('C2', '[email protected]');

// Save the spreadsheet as a new file
$writer = new Xlsx($spreadsheet);
$writer->save('newfile.xlsx');

// READ
// Load the spreadsheet from a file
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('existingfile.xlsx');
$sheet = $spreadsheet->getActiveSheet();

// Get the data from the sheet
$data = $sheet->toArray();

// Print the data
print_r($data);

// UPDATE
// Load the spreadsheet from a file
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('existingfile.xlsx');
$sheet = $spreadsheet->getActiveSheet();

// Update the data in the sheet
$sheet->setCellValue('B2', 'Jane Doe');

// Save the changes
$writer = new Xlsx($spreadsheet);
$writer->save('existingfile.xlsx');

// DELETE
// Load the spreadsheet from a file
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('existingfile.xlsx');
$sheet = $spreadsheet->getActiveSheet();

// Delete a row
$sheet->removeRow(2);

// Save the changes
$writer = new Xlsx($spreadsheet);
$writer->save('existingfile.xlsx');

Here is the documentation for PhpSpreadsheet library. Read the docs for learning, creating, reading, updating, and deleting CSV files in PHP.