konrms's avatar

Help with csv file uploading...

Hello guys. I'm trying to upload a .csv file to my (oracle) database. In fact the end user should insert this file along with other data. After submitting, a pdf file should be created.

I have created class CreateCsvDataTable (php migrate) with schema csv_data and the table csv_data exists in my oracle. It should be populated without headers.

My trouble is with csv uploading (please have a look at point (3)). When giving dd($header); I get this error:

Call to a member function getRealPath() on null

Could you please assist?

(1) My web.php routes file is the following:

Route::get('/diploma', 'PdfController@quest');
Route::post('/hard', 'PdfController@job');

Route::get('/p', 'PdfController@pointer');
Route::get('/export1','PdfController@export1')->name('export1.pdf');

(2) The form code (blade excerpt) with data requested is this:

    <form method='post' action="/hard">
    
        {{csrf_field()}}
        

        
        <section>   
        Choose program:
            <select name="sc" id="xaos">
        
            <optgroup label="postgraduates">
                @foreach($transport as $y)
                    <option value="{{$y->object_id}}">{{$y->object_name}}</option>
                @endforeach
            </optgroup>
        
            </select>
        </section>
    
    <br>

        <section>
        AM: 
            <input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Value < 1999999')">
        </section>
        
        <br>
        
        <section>
        Select language:
            <select name="language" id="lang">
                    <option value="GR"> Greek</option>
                    <option value="EN"> English</option>
            </select>
        </section>
        
        <br>
        
        <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file" class="form-control">
        </div>
        <input class="btn btn-success" type="submit" value="Upload " name="submit">

        <section>
        <br>
            <input type='submit' value="Submit!">
        </section>
        
    </form>

(3) And the PdfController.php excerpt regarding form input data get is this:

public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //works!
        $a2 = $p -> get('am');  //works!
        $a3 = $p -> get('language');  //works!
        
        //get csv file                          
        $upload = $p -> file('upload-file');
        $filePath = $upload ->getRealPath(); --> HELP NEEDED HERE!!
        
        //open and read file
        $file = fopen($filePath, 'r');
        $header = fgetcsv($file); //I want to insert data without header
        
        dd($header);
0 likes
11 replies
devfrey's avatar

Your <form> tag is missing an important attribute: enctype="multipart/form-data".

konrms's avatar

@DEVFREY - Hello @devfrey ... Thanks for replying! I have updated form according to your recommendation. The problems persist though. I'm trying to stick to this guide https://justlaravel.com/import-csv-data-store-database/?utm_source=learninglaravel.net but now I get Undefined offset: 8 error. What could that be?

To sum up, all my actions so far are:

I have run php artisan make:migration create_csv_data_table at cmd I created the table fields to database\migrations csv_data_table I have migrated this to database ( php artisan migrate) and the table csv_data is created into my oracle db I have also created the correspondig model (with php artisan make:model Csvdata)

And below some code excerpts relative to the issue:

  1. web.php
Route::get('/diploma', 'PdfController@quest');
Route::post('/hard', 'PdfController@job');

Route::get('/p', 'PdfController@pointer');
Route::get('/export1','PdfController@export1')->name('export1.pdf');
  1. form:
<form method='post' enctype="multipart/form-data" action="/hard">

        {{csrf_field()}}

        <section>
        <br>
            <legend><i><b> Συμπλήρωση Στοιχείων Παραρτήματος Διπλώματος</b></i></legend>
        <br>
        </section>

        <section>   
        Επιλογή Προγράμματος Σπουδών:
            <select name="sc" id="xaos">

            <optgroup label="postgraduates">
                @foreach($transport as $y)
                    <option value="{{$y->object_id}}">{{$y->object_name}}</option>
                @endforeach
            </optgroup>

            </select>
        </section>

    <br>

        <section>
        Αριθμός Μητρώου Φοιτητή: 
            <input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Τιμή < 1999999')">
        </section>

        <br>

        <section>
        Επιλογή Γλώσσας:
            <select name="language" id="lang">
                    <option value="GR"> Greek</option>
                    <option value="EN"> English</option>
            </select>
        </section>

        <br>

        <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file" class="form-control">
        </div>
        <input class="btn btn-success" type="submit" value="Upload " name="submit">

        <section>
        <br>
            <input type='submit' value="Submit!">
        </section>

    </form>
  1. migrations file class:
class CreateCsvDataTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('csv_data', function (Blueprint $table) 
        {
            $table->integer('ac1')-> nullable();
            $table->string('ac2')-> nullable();
            $table->string('ac3')-> nullable();
            $table->integer('ac4')-> nullable();
            $table->integer('ac5')-> nullable();
            $table->string('ac6')-> nullable();
            $table->integer('ac7')-> nullable();
            $table->string('ac8')-> nullable();
            $table->string('ac9')-> nullable();
            $table->integer('ac10')-> nullable();
            $table->integer('ac11')-> nullable();
            $table->integer('ac12')-> nullable();
            $table->integer('ac13')-> nullable();
            $table->float('ac14')-> nullable();
            $table->integer('ac15')-> nullable();
            $table->integer('ac16')-> nullable();
            $table->integer('ac17')-> nullable();
            $table->string('ac18')-> nullable();
            $table->string('ac19')-> nullable();
            $table->string('ac20')-> nullable();
            $table->string('ac21')-> nullable();
            $table->integer('ac22')-> nullable();
            $table->float('ac23')-> nullable();
            $table->string('ac24')-> nullable();
            $table->float('ac25')-> nullable();
            $table->float('ac26')-> nullable();
            $table->string('ac27')-> nullable();
            $table->float('ac28')-> nullable();
            $table->string('ac29')-> nullable();
            $table->string('ac30')-> nullable();
            $table->string('ac31')-> nullable();
            $table->string('ac32')-> nullable();
            $table->integer('ac33')-> nullable();
            $table->string('ac34')-> nullable();
            $table->string('ac35')-> nullable();
            $table->string('ac36')-> nullable();
            $table->string('ac37')-> nullable();
            $table->string('ac38')-> nullable();
            $table->string('ac39')-> nullable();
            $table->string('ac40')-> nullable();
            $table->string('ac41')-> nullable();
            $table->integer('ac42')-> nullable();
            $table->date('ac43')-> nullable();
            $table->string('ac44')-> nullable();
            $table->string('ac45')-> nullable();
            $table->string('ac46')-> nullable();
            $table->string('ac47')-> nullable();
            $table->string('ac48')-> nullable();
            $table->string('ac49')-> nullable();
            $table->string('ac50')-> nullable();
            $table->string('ac51')-> nullable();
            $table->string('ac52')-> nullable();
            $table->string('ac53')-> nullable();
            $table->string('ac54')-> nullable();
            $table->integer('ac55')-> nullable();
            $table->integer('ac56')-> nullable();
            $table->integer('ac57')-> nullable();
            $table->integer('ac58')-> nullable();
            $table->integer('ac59')-> nullable();
            $table->string('ac60')-> nullable();
            $table->string('ac61')-> nullable();
            $table->string('ac62')-> nullable();
            $table->integer('ac63')-> nullable();
            $table->date('ac64')-> nullable();
            $table->date('ac65')-> nullable();
            $table->date('ac66')-> nullable();
            $table->string('ac67')-> nullable();
            $table->integer('ac68')-> nullable();
            $table->string('ac69')-> nullable();
            $table->integer('ac70')-> nullable();
            $table->integer('ac71')-> nullable();
            $table->string('ac72')-> nullable();
            $table->string('ac73')-> nullable();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('csv_data');
    }
}
  1. Controller excerpt (when pressing upload at my form I get Undefined offset: 8 error and line $csv_data->ac9 =$data [8]; is marked)
public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //όνομα προγράμματος
        $a2 = $p -> get('am');  //αριθμός μητρώου φοιτητή
        $a3 = $p -> get('language');  //γλώσσα

        if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 
        {
            while ( ($data = fgetcsv ( $handle, 1000, ',' )) !== FALSE )
                {
                    {
                        $csv_data = new Csvdata ();
                        $csv_data->ac1  =$data [0];
                        $csv_data->ac2  =$data [1];
                        $csv_data->ac3  =$data [2];
                        $csv_data->ac4  =$data [3];
                        $csv_data->ac5  =$data [4];
                        $csv_data->ac6  =$data [5];
                        $csv_data->ac7  =$data [6];
                        $csv_data->ac8  =$data [7];
                        $csv_data->ac9  =$data [8];
                        $csv_data->ac10 =$data [9];
                        $csv_data->ac11 =$data [10];
                        $csv_data->ac12 =$data [11];
                        $csv_data->ac13 =$data [12];
                        $csv_data->ac14 =$data [13];
                        $csv_data->ac15 =$data [14];
                        $csv_data->ac16 =$data [15];
                        $csv_data->ac17 =$data [16];
                        $csv_data->ac18 =$data [17];
                        $csv_data->ac19 =$data [18];
                        $csv_data->ac20 =$data [19];
                        $csv_data->ac21 =$data [20];
                        $csv_data->ac22 =$data [21];
                        $csv_data->ac23 =$data [22];
                        $csv_data->ac24 =$data [23];
                        $csv_data->ac25 =$data [24];
                        $csv_data->ac26 =$data [25];
                        $csv_data->ac27 =$data [26];
                        $csv_data->ac28 =$data [27];
                        $csv_data->ac29 =$data [28];
                        $csv_data->ac30 =$data [29];
                        $csv_data->ac31 =$data [30];
                        $csv_data->ac32 =$data [31];
                        $csv_data->ac33 =$data [32];
                        $csv_data->ac34 =$data [33];
                        $csv_data->ac35 =$data [34];
                        $csv_data->ac36 =$data [35];
                        $csv_data->ac37 =$data [36];
                        $csv_data->ac38 =$data [37];
                        $csv_data->ac39 =$data [38];
                        $csv_data->ac40 =$data [39];
                        $csv_data->ac41 =$data [40];
                        $csv_data->ac42 =$data [41];
                        $csv_data->ac43 =$data [42];
                        $csv_data->ac44 =$data [43];
                        $csv_data->ac45 =$data [44];
                        $csv_data->ac46 =$data [45];
                        $csv_data->ac47 =$data [46];
                        $csv_data->ac48 =$data [47];
                        $csv_data->ac49 =$data [48];
                        $csv_data->ac50 =$data [49];
                        $csv_data->ac51 =$data [50];
                        $csv_data->ac52 =$data [51];
                        $csv_data->ac53 =$data [52];
                        $csv_data->ac54 =$data [53];
                        $csv_data->ac55 =$data [54];
                        $csv_data->ac56 =$data [55];
                        $csv_data->ac57 =$data [56];
                        $csv_data->ac58 =$data [57];
                        $csv_data->ac59 =$data [58];
                        $csv_data->ac60 =$data [59];
                        $csv_data->ac61 =$data [60];
                        $csv_data->ac62 =$data [61];
                        $csv_data->ac63 =$data [62];
                        $csv_data->ac64 =$data [63];
                        $csv_data->ac65 =$data [64];
                        $csv_data->ac66 =$data [65];
                        $csv_data->ac67 =$data [66];
                        $csv_data->ac68 =$data [67];
                        $csv_data->ac69 =$data [68];
                        $csv_data->ac70 =$data [69];
                        $csv_data->ac71 =$data [70];
                        $csv_data->ac72 =$data [71];
                        $csv_data->ac73 =$data [72];

                        $csv_data->save ();
                    }
                    fclose ( $handle );
                }
                $finalData = $csv_data::all ();
                dd($finalData);
devfrey's avatar

@KONRMS - If the lines in your CSV are long, it's possible the fgetcsv() function splits them up. I suggest you change the second parameter from 1000 to 0, which removes the limit. It might be a bit slower, but it may fix your problem.

Also, your hydration code

$csv_data->ac1 = $data[0];
$csv_data->ac2 = $data[1];
// etc.

can be simplified into this:

foreach(range(0, 72) as $index) {
    $csv_data->{'ac'.($index + 1)} = $data[$index];
}
konrms's avatar

@DEVFREY - I altered code according your recommendation. I'm getting again "Undefined offset: 8". The error page now highlights code line.

$csv_data->{'ac'.($index + 1)} = $data[$index];

The code excerpt is this. What else could I check?

    public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //όνομα προγράμματος
        $a2 = $p -> get('am');  //αριθμός μητρώου φοιτητή
        $a3 = $p -> get('language');  //γλώσσα
        
        if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 
        {
            while ( ($data = fgetcsv ( $handle, 0, ',' )) !== FALSE )
                {
                    {
                        $csv_data = new Csvdata ();
                        
                        foreach(range(0, 72) as $index) 
                        {
                            $csv_data->{'ac'.($index + 1)} = $data[$index];
                        }

                        
                        $csv_data->save ();
                    }
                    fclose ( $handle );
                }
                $finalData = $csv_data::all ();
                dd($finalData);

........................
devfrey's avatar

@KONRMS - Odd – that means $data doesn't go past index 7. Could you try doing dd($data) right after the while loop starts?

Edit: sorry, code formatting is all messed up...

konrms's avatar

@DEVFREY - Never mind! Once again, thanks for helping!

I changed code as follows:

public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //όνομα προγράμματος
        $a2 = $p -> get('am');  //αριθμός μητρώου φοιτητή
        $a3 = $p -> get('language');  //γλώσσα
        
        if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 
        {
            while ( ($data = fgetcsv ( $handle, 0, ',' )) !== FALSE )
                dd($data);

The result I'm getting is below. Why those strange characters appear? They represent greek language words in .csv.

array:8 [▼
  0 => b"1040290;ÐÁÐÁÄÇÌÇÔÑÉÏÕ"
  1 => b" ÃÅÙÑÃÉÏÓ;ÊÙÍÓÔÁÍÔÉÍÏÓ;234513;9198;Áðüöïéôïò;5;STC_131;ÄÉÁ×. ÓÕÍÈ. ÁÍÔÉÊ. ÐÏË.-ÌÅÓÙÍ ÓÅ Â. Ä.;2014;20;2014;20;9"
  2 => b"0;;2;1;Ð;ËÕ;ÕÐÏ;;6"
  3 => "00;0"
  4 => b"00;ÐÌ;0"
  5 => "00;0"
  6 => b"00;ÄÌ;0"
  7 => b"0;EG0STC_S2R1;ËÕ 1ï ÅîÜìçíï Êïñìïý;EG0STC_SPEC;Êáôåõèýíóåéò  ÅðéóôÞìçò &Ôå÷íïëïãßáò Ç/Õ;1;PR;Äåßêôçò äåí Êáèïñßóôçêå;Äåßêôçò äåí Êáèïñßóôçêå;Äåßêôçò äåí Êáèïñßóôçêå;Äåßêôçò äåí Êáèïñßóôçêå;Ìç Ìåôáöåñüìåíï;Ï÷é;Åêðñüèåóìç;1;27/03/2015;Ìç ÁôïìéêÞ Åñãáóßá;0;;;EMBK;00155D6E0B0B1ED4AAD6D796E8DA83E3;00155D6E0B0B1ED4AAD6D796E8DA63E3;00155D6E0B0B1ED4AAD6D796E8DA63E3;50091351;EG0_STC;Ð.Ì.Ó. ÅðéóôÞìç &Ôå÷í ÇÕ (-2017);50086330;50091396;0;50091374;50091355;;;;0;01/09/2014;15/02/2015;02/02/2015;10:37:36 ðì;0;;2;50098572;00155D6E0B0B1ED4AAD6D796E8D8A3E3; ◀"
]
devfrey's avatar

@KONRMS - Then there's your problem: character encoding.

I don't know how this could easily be solved with PHP's native functions, but I know League's CSV library has support for charset conversion: https://csv.thephpleague.com/9.0/converter/charset/

If you opt for this – in my opinion more robust solution – you will have to rewrite your CSV code.

Edit: also, are you sure your CSV is comma-delimited? I see a lot of semicolons in your dump.

konrms's avatar

@DEVFREY - I have found that replacing ',' delimiter with ';' at my code excerpt

if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 
        {
            while ( ($data = fgetcsv ( $handle, 0, ';' )) !== FALSE )
                dd($data);

and dd($data) fills all data. Just like below. The odd characters remain though.

array:73 [▼
  0 => "1040290"
  1 => b"ÐÁÐÁÄÇÌÇÔÑÉÏÕ, ÃÅÙÑÃÉÏÓ"
  2 => b"ÊÙÍÓÔÁÍÔÉÍÏÓ"
  3 => "234513"
  4 => "9198"
  5 => b"Áðüöïéôïò"
  6 => "5"
  7 => "STC_131"
  8 => b"ÄÉÁ×. ÓÕÍÈ. ÁÍÔÉÊ. ÐÏË.-ÌÅÓÙÍ ÓÅ Â. Ä."
  9 => "2014"
  10 => "20"
  11 => "2014"
  12 => "20"
  13 => "9,0"
  14 => ""
  15 => "2"
  16 => "1"
  17 => b"Ð"
  18 => b"ËÕ"
  19 => b"ÕÐÏ"
  20 => ""
  21 => "6,00"
  22 => "0,00"
  23 => b"ÐÌ"
  24 => "0,00"
  25 => "0,00"
  26 => b"ÄÌ"
  27 => "0,0"
  28 => "EG0STC_S2R1"
  29 => b"ËÕ 1ï ÅîÜìçíï Êïñìïý"
  30 => "EG0STC_SPEC"
  31 => b"Êáôåõèýíóåéò  ÅðéóôÞìçò &Ôå÷íïëïãßáò Ç/Õ"
  32 => "1"
  33 => "PR"
  34 => b"Äåßêôçò äåí Êáèïñßóôçêå"
  35 => b"Äåßêôçò äåí Êáèïñßóôçêå"
  36 => b"Äåßêôçò äåí Êáèïñßóôçêå"
  37 => b"Äåßêôçò äåí Êáèïñßóôçêå"
  38 => b"Ìç Ìåôáöåñüìåíï"
  39 => b"Ï÷é"
  40 => b"Åêðñüèåóìç"
  41 => "1"
  42 => "27/03/2015"
  43 => b"Ìç ÁôïìéêÞ Åñãáóßá"
  44 => "0"
  45 => ""
  46 => ""
  47 => "EMBK"
  48 => "00155D6E0B0B1ED4AAD6D796E8DA83E3"
  49 => "00155D6E0B0B1ED4AAD6D796E8DA63E3"
  50 => "00155D6E0B0B1ED4AAD6D796E8DA63E3"
  51 => "50091351"
  52 => "EG0_STC"
  53 => b"Ð.Ì.Ó. ÅðéóôÞìç &Ôå÷í ÇÕ (-2017)"
  54 => "50086330"
  55 => "50091396"
  56 => "0"
  57 => "50091374"
  58 => "50091355"
  59 => ""
  60 => ""
  61 => ""
  62 => "0"
  63 => "01/09/2014"
  64 => "15/02/2015"
  65 => "02/02/2015"
  66 => b"10:37:36 ðì"
  67 => "0"
  68 => ""
  69 => "2"
  70 => "50098572"
  71 => "00155D6E0B0B1ED4AAD6D796E8D8A3E3"
  72 => ""
]

But when I remove dd and try to run code on the whole the error changes to this:

Error Code : 942 Error Message : ORA-00942: table or view does not exist Position : 12 Statement : insert into "CSVDATA" ("AC1", "AC2", "AC3", "AC4", "AC5", "AC6", "AC7", "AC8", "AC9", "AC10", "AC11", "AC12", "AC13", "AC14", "AC15", "AC16", "AC17", "AC18", "AC19", "AC20", "AC21", "AC22", "AC23", "AC24", "AC25", "AC26", "AC27", "AC28", "AC29", "AC30", "AC31", "AC32", "AC33", "AC34", "AC35", "AC36", "AC37", "AC38", "AC39", "AC40", "AC41", "AC42", "AC43", "AC44", "AC45", "AC46", "AC47", "AC48", "AC49", "AC50", "AC51", "AC52", "AC53", "AC54", "AC55", "AC56", "AC57", "AC58", "AC59", "AC60", "AC61", "AC62", "AC63", "AC64", "AC65", "AC66", "AC67", "AC68", "AC69", "AC70", "AC71", "AC72", "AC73") values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18, :p19, :p20, :p21, :p22, :p23, :p24, :p25, :p26, :p27, :p28, :p29, :p30, :p31, :p32, :p33, :p34, :p35, :p36, :p37, :p38, :p39, :p40, :p41, :p42, :p43, :p44, :p45, :p46, :p47, :p48, :p49, :p50, :p51, :p52, :p53, :p54, :p55, :p56, :p57, :p58, :p59, :p60, :p61, :p62, :p63, :p64, :p65, :p66, :p67, :p68, :p69, :p70, :p71, :p72) returning "ID" into :p73
devfrey's avatar

@KONRMS - Your query uses table 'CSVDATA', while your migration has 'csv_data'.

I suggest you take a look at the CSV library I provided. That should help you solve your encoding problems.

konrms's avatar

@DEVFREY - Hello again!

I had some issues with my csv numbers and had to update ',' to '.' in numbers (eg 7,5 to 7.5). I think that can be fixed somehow...

But my basic issue is how to pass the file from the form to my controller. The user should enter some other data along with the csv. The csv is uploaded only if it is placed inside public folder and in fact code does not take in mind my (desktop located) file. The code as it is takes file from public folder and successfully inserts it into my database. Could you give me your advice on this point please?

This is my form excerpt. At the bottom is where I ask user to upload file:

<body>

    <form method='post' enctype="multipart/form-data" action="/hard">

        {{csrf_field()}}

        <section>
        <br>
            <legend><i><b> Complete Data Below</b></i></legend>
        <br>
        </section>

        <section>   
        Choose program:
            <select name="sc" id="xaos">

            <optgroup label="postgraduates">
                @foreach($transport as $y)
                    <option value="{{$y->object_id}}">{{$y->object_name}}</option>
                @endforeach
            </optgroup>

            </select>
        </section>

    <br>

        <section>
        ID number: 
            <input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Value < 1999999')">
        </section>

        <br>

        <section>
        Select Language:
            <select name="language" id="lang">
                    <option value="GR"> Greek</option>
                    <option value="EN"> English</option>
            </select>
        </section>
        <br>
        <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file"> 
        </div>
            <input type='submit' name='upload' value="Submit!">
        </section>

    </form>
    <br>
    <br>

</body>

And below is my controller excerpt which is supposed to receive file. The issue is with fopen function. How can it handle the file imported from the form above?

public function job(Request $p)
    {
        $a1 = $p -> get('sc');
        $a2 = $p -> get('am'); 
        $a3 = $p -> get('language'); 




    $f_rownum = 0;

    if (($handle = fopen ( 'MOCK_DATA.csv', 'r' )) !== FALSE) 

    {
        while ( ($data = fgetcsv ( $handle, 1000, ';' )) !== FALSE )
        {
            $ac1=$data[0];
            $ac2=iconv("Windows-1253", "UTF-8", $data[1]);
            $ac3=iconv("Windows-1253", "UTF-8", $data[2]);
            $ac4=iconv("Windows-1253", "UTF-8", $data[3]);
            $ac5=$data[4];

......
konrms's avatar
konrms
OP
Best Answer
Level 1

The answer on the whole:

  1. Html form excerpt which asks user to input the .csv file:
<!DOCTYPE html>

<html>
<meta charset="UTF-8">
<head>


<title>test</title>

<style>
    section
    {
        text-align: justify;
        padding-top: 1%;
            padding-left: 35%;
            padding-right: 25%;        
    }
</style>
</head>
<body>

     <form method='post' enctype="multipart/form-data" action="/hard">
        
        {{csrf_field()}}

    <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file"">
        </div>
        <br>
        <input type="submit" value="Submit " name="submit">

        </section>
    .....................

  1. Controller function excerpt. The file is received and checked if it has the right extension (.csv) or it is empty. iconv is needed for correct greek language encoding (refer to my previous answer).
public function job(Request $p)
    {
        $nrg = DB::delete('delete from csv_data');  //clear csv_data table
                
        $upload = $p -> file('upload-file'); 

if ($upload==null)
                {
                    return view ('search_again');
                }
                else
                    {
                        $extension = $p->file('upload-file')->getClientOriginalExtension();
                        if ($extension !=='csv')
                            {
                                return view ('search_again');
                            }
                    }
        
            $filePath = $upload ->getRealPath();  //upload file path
        
            $file = fopen($filePath, 'r');


            $f_rownum = 0;

            if (($handle = fopen ( $filePath, 'r' )) !== FALSE) 
                {
                    while ( ($data = fgetcsv ( $handle, 1000, ',' )) !== FALSE )
                        {
                            $ac1=iconv("Windows-1253", "UTF-8", $data[0]);
                            $ac2=iconv("Windows-1253", "UTF-8", $data[1]);
                            $ac3=iconv("Windows-1253", "UTF-8", $data[2]);
                            ........
                            $ac72=iconv("Windows-1253", "UTF-8", $data[71]);
                            $ac73=iconv("Windows-1253", "UTF-8", $data[72]);

                            DB::table('csv_data')->insert([
                            'ac1'=> $ac1, 'ac2'=>$ac2,'ac3'=>$ac3, 'ac4'=>$ac4,
                            ..........,'ac73'=>$ac73]);
                            
$f_rownum++;
                            }
                            
                    fclose ( $handle );
            }

Please or to participate in this conversation.