if (!empty($request->files) && $request->hasFile('csv_file')) {
$file = $request->file('csv_file');
$type = $file->getClientOriginalExtension();
$real_path = $file->getRealPath();
if ($type <> 'csv') {
Alert::error('Wrong file extension', 'Only CSV is allowed')->persistent('close');
return redirect()->back();
}
$data = $this->readCSV($real_path, array('delimiter' => ','));
}
function csvToArray($filename = '', $delimiter = ',')
{
if (!file_exists($filename) || !is_readable($filename))
return false;
$header = null;
$data = array();
if (($handle = fopen($filename, 'r')) !== false)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false)
{
if (!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
<?php
function joinFiles(array $files, $result) {
if(!is_array($files)) {
throw new Exception('`$files` must be an array');
}
$wH = fopen($result, "w+");
foreach($files as $file) {
$fh = fopen($file, "r");
while(!feof($fh)) {
fwrite($wH, fgets($fh));
}
fclose($fh);
unset($fh);
fwrite($wH, "
"); //usually last line doesn't have a newline
}
fclose($wH);
unset($wH);
}