Your <form> tag is missing an important attribute: enctype="multipart/form-data".
May 18, 2019
11
Level 1
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);
Level 1
The answer on the whole:
- 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>
.....................
- 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.