Tuesday, February 28, 2017

Unable to Import comma separated file (laravel 5.3)

I have 3 types of file, they can be comma separated names as

abc,xyz or it can be as abc xyz or also can be as abc,xyz abcd

I want to import above all formats in DB.At this time I am able to import only 2nd format.In other cases it imports only 1st name

My code is as follows:

$file = $request->file('file');

$name = $file->getClientOriginalName();
if ($file->extension() == 'txt' || $file->extension() == 'csv') {
   $destination = config('school.students.names');
   $file->move($destination, $name);
   chmod($destination. $name, 0777);

$delimiter = ($request->input('file') == 'csv') ? "," : "\t";
   $handle = fopen($destination.$name, 'r');
   while (($line = fgetcsv($handle, $delimiter)) !== false) {
   $name = $line;
   $data['students']   = $name[0];

   $name = Students::create($data);
 }

This time its only export 2nd format data please help to fix for others format also.

Thanks




via recovery men

Advertisement