<input type="file" name="excel[]" multiple class="form-control">
public function store(Request $request) {
$message = "";
if($request->hasFile('excel')) {
foreach($files as $file){
$link = 1;
$success = $this->fileGetContentsChunked($link, $file->getRealPath(), 2048, '', function ($chunk, &$handle, $iteration, &$queryValuePrefix, $link) {
$TABLENAME = 'table_name';
$chunk = $queryValuePrefix . $chunk;
$lineArray = preg_split("/\r\n|\n|\r/", $chunk);
$query = 'INSERT INTO ' . $TABLENAME . '(fiend1, fiend2, fiend3, fiend4, fiend5, fiend6, fiend7, fiend8, fiend9) VALUES ';
$numberOfRecords = count($lineArray);
for ($i = 0; $i < $numberOfRecords - 2; $i ++) {
$colArray = explode(',', $lineArray[$i]);
$val1 = (isset($colArray[1])) ? addslashes($colArray[1]) : "";
$val2 = (isset($colArray[4])) ? addslashes($colArray[4]) : "";
$val3 = (isset($colArray[6])) ? addslashes($colArray[6]) : "";
$val4 = (isset($colArray[7])) ? addslashes($colArray[7]) : "";
$val5 = (isset($colArray[10])) ? addslashes($colArray[10]) : "";
$val6 = (isset($colArray[11])) ? addslashes($colArray[11]) : "";
$val7 = (isset($colArray[31])) ? addslashes($colArray[31]) : "";
$val8 = (isset($colArray[55])) ? addslashes($colArray[55]) : "";
$val9 = (isset($colArray[87])) ? addslashes($colArray[87]) : "";
$query = $query . '("' . $val1 . '","' . $val2 . '","' . $val3. '","' . $val4 . '","' . $val5 . '","' . $val6 . '","' . $val7 . '","' . $val8 . '","' . $val9 . '"),';
}
$colArray = explode(',', $lineArray[$i]);
$val1 = (isset($colArray[1])) ? addslashes($colArray[1]) : "";
$val2 = (isset($colArray[4])) ? addslashes($colArray[4]) : "";
$val3 = (isset($colArray[6])) ? addslashes($colArray[6]) : "";
$val4 = (isset($colArray[7])) ? addslashes($colArray[7]) : "";
$val5 = (isset($colArray[10])) ? addslashes($colArray[10]) : "";
$val6 = (isset($colArray[11])) ? addslashes($colArray[11]) : "";
$val7 = (isset($colArray[31])) ? addslashes($colArray[31]) : "";
$val8 = (isset($colArray[55])) ? addslashes($colArray[55]) : "";
$val9 = (isset($colArray[87])) ? addslashes($colArray[87]) : "";
$query = $query . '("' . $val1 . '","' . $val2 . '","' . $val3. '","' . $val4 . '","' . $val5 . '","' . $val6 . '","' . $val7 . '","' . $val8 . '","' . $val9 . '")';
$i = $i + 1;
$queryValuePrefix = $lineArray[$i];
DB::insert(DB::raw($query));
});
}
}
}
function fileGetContentsChunked($link, $file, $chunk_size, $queryValuePrefix, $callback)
{
try {
$handle = fopen($file, "r");
$i = 0;
while (! feof($handle)) {
call_user_func_array($callback, array(
fread($handle, $chunk_size),
&$handle,
$i,
&$queryValuePrefix,
$link
));
$i ++;
}
fclose($handle);
} catch (Exception $e) {
trigger_error("file_get_contents_chunked::" . $e->getMessage(), E_USER_NOTICE);
return false;
}
return true;
}