Last active
July 26, 2020 12:03
-
-
Save surferxo3/cf8897bbf4740600ecd62dc12f7672be to your computer and use it in GitHub Desktop.
Csv file upload with support of handling files generated via Macintosh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*############################# | |
* Developer: Mohammad Sharaf Ali | |
* Designation: Sr. SE | |
* Version: 1.0 | |
*/############################# | |
public static function processBulkImport($request) | |
{ | |
try { | |
ini_set('max_execution_time', 0); | |
ini_set('memory_limit', -1); | |
ini_set('auto_detect_line_endings', 1); // to parse Mac csv file(s) | |
$file = $request->file('csv_file'); | |
$handle = fopen($file->getRealPath(), 'r'); | |
$header = ['username', 'phoneNo', 'companyCode', 'userCode']; | |
$data = ['notifyEmail' => $request->notify_email, 'data' => []]; | |
$error = $response = null; | |
if (count(fgetcsv($handle)) == 4) { | |
while (($row = fgetcsv($handle, 1000, ',')) !== false) { | |
if (!empty($row) && count($row) == count(array_filter($row))) { | |
$data['data'][] = array_combine($header, array_map('trim', $row)); | |
} else { | |
$error = 'messages.empty_cell_value'; | |
break; | |
} | |
} | |
} else { | |
$error = 'messages.invalid_header_count'; | |
} | |
fclose($handle); | |
if ($error) { | |
$response = self::error($error); | |
} else { | |
$response = self::postBulkImport($data); | |
} | |
} catch (\Exception $e) { | |
Log::error("processBulkImport() there is some exception " . $e->getMessage()); | |
$response = self::error(); | |
} | |
return (array) $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment