Andreea@Webclix's avatar

uploading CSV file failing

For a customer I need to upload a cvs file. The file has nearly 35000 lines. I used maatwebsite/excel package.

Excel::filter('chunk')->load($file->getRealPath())->chunk(100, 
    function($results) {
        foreach ($results as $row) {
            // Doing the  import in the DB
        }
    }
});

I can't change the max_execution_time because our server doesn't allow executions more than 300 seconds.

I tried also ried another way without any package but that failed also.

$csv = utf8_encode(file_get_contents($file));
$array = explode("\n", $csv);

foreach ($array as $key => $row) {
    if($key == 0) {
        $head = explode(',', $row);

        foreach ($head as $k => $item) {
            $h[$key][] = str_replace(' ', '_', $item);
        }
    }
    if($key != 0) {
        $product = explode(',', $row);

        foreach ($product as $k => $item) {
            if($k < 21)
                $temp[$key][$h[0][$k]] = $item;
        }
    }
}

foreach ($temp as $key => $value) {
    // Doing the  import in the DB
}

Does anyone have an idea

0 likes
10 replies
Andreea@Webclix's avatar

I found a solution where I made an artisan command. When I run the command the file is imported in the database without timeout.

I now need to know how I can execute the command after a file upload in a controller. The command should run asynchrone so the user can close his browser while it executes.

Does anyone have ideas?

Cronix's avatar

I now need to know how I can execute the command after a file upload in a controller. The command should run asynchrone so the user can close his browser while it executes.

You just described queues: https://laravel.com/docs/5.5/queues

Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.

Andreea@Webclix's avatar

Yes I know and I tried that but I don't seem to be able to get it working

App\Jobs\ProcessImport::dispatch();

public function handle()
{
    Artisan::call('csv:import');
}
Andreea@Webclix's avatar

I have run the artisan command in console, work perfect so that is not the issue.

LaraStorm's avatar

sample working code

if($request->hasFile('file')){
            $extension = File::extension($request->file->getClientOriginalName());
            if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
 
                $path = $request->file->getRealPath();
                $data = Excel::load($path, function($reader) {
                })->get();
                
                if(!empty($data) && $data->count()){
 
                     foreach ($data as $key => $row) {
                             $insert[] = [
                                 'customer_name' => $row->contact,
                                 'customer_address' => $row->full_address,
                                 'product_name' => implode(' ', array_unique(explode(' ', strtok($row->products_name, "\",.,")))),
                                 'date_send' => $row->date_send
                             ];
                    }

                    if(!empty($insert)){
                        $vv = 0;
                        for ($i=0; $i < sizeof($insert) ; $i++) { 
                            if (orders::where('waybill_id',$insert[$i]['waybill_id'])->exists()) {

                            }else {
                                DB::table('orders')->insert($insert[$i]);
                                $vv++ ;
                            }
                        }
                        
                    }
                }

                Session::flash('success', $vv.' '.'Records have been Imported/updated ');
                return redirect('admin/orders');
 
            }else {
                Session::flash('error', 'File is a '.$extension.' failed');
                return back();
            }
        }
    }

Please or to participate in this conversation.