PHP code example of onlyphp / codeigniter3-csvimporter
1. Go to this page and download the library: Download onlyphp/codeigniter3-csvimporter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
onlyphp / codeigniter3-csvimporter example snippets
// Initialize the processor
$processor = new \OnlyPHP\CSVSimpleImporter\CSVImportProcessor();
// Set callback function for processing each row
$processor->setCallback(function($row, $rowIndex, $models) {
try {
// Process your row data here
return [
'code' => 200,
'action' => 'create',
'message' => 'Success'
];
} catch (\Exception $e) {
return [
'code' => 500,
'error' => $e->getMessage()
];
}
});
// Start processing
$jobId = $processor->process('/path/to/your/file.csv');
$processor = new \OnlyPHP\CSVSimpleImporter\CSVImportProcessor();
// Set user ID for file ownership
$processor->setFileBelongsTo(1);
// Set HTML element ID for frontend progress tracking
$processor->setDisplayHTMLId('progress-bar-1');
// Configure CSV processing parameters
$processor->setMemoryLimit('1G')
->setDelimiter(',')
->setEnclosure('"')
->setEscape('\\')
->setChunkSize(1000)
->setRecordUpdateInterval(250)
->setSkipHeader(true);
// Load specific models for processing
$processor->setCallbackModel(['User_model', 'Product_model']);
// Set callback with loaded models
$processor->setCallback(function($row, $rowIndex, $models) {
$userModel = $models['User_model'];
$productModel = $models['Product_model'];
try {
// Your processing logic here
$result = $userModel->createFromCSV($row);
return [
'code' => 200,
'action' => 'create',
'message' => 'User created successfully'
];
} catch (\Exception $e) {
return [
'code' => 500,
'error' => 'Row ' . $rowIndex . ': ' . $e->getMessage()
];
}
});
// Start processing
$jobId = $processor->process('/path/to/your/encryptFileName.csv', 'originalFileName.csv');
// Kill a running process
$processor->killProcess($jobId);
// Check process status
$status = $processor->getStatus($jobId);
// Check status for all processes owned by a user
$status = $processor->getStatusByOwner($userId);