Steps to do a file upload in Laravel Vapor

id: 3958 name: vapor-example environments: Production: storage: vapor-blog build: - 'composer install --no-dev' - 'php artisan event:cache' - 'npm ci && npm run prod && rm -rf node_modules' deploy: - 'php artisan migrate --force'
Route::get('/upload', function () { return view('upload'); }); Route::post('/upload', 'UploadController@store');
<form method="post" enctype="multipart/form-data"> {{csrf_field()}} <div class="form-group"> <label for="exampleInputFile">File input</label> <input type="file" id="exampleInputFile" name="input_file"> Upload CSV File. </div> <button type="submit" class="btn btn-default">Submit</button> </form>
/** * Stores the file and convert to array * @param Request $request * @return dumped data */ public function store(Request $request) { $fileName = date("m-d-y-H-i-s") . '.csv'; $path = $request->file('input_file')-&amp;gt;storeAs('uploads', $fileName); $items = $this->processCsv($path); dd($items); }
/** * Process Csv File and convert to an array * @param string $path * @return array */ public function processCsv(string $path) { if (!Storage::exists($path)) { return false; } $csvFile = Storage::get($path); $csvData = $this->convertCsvToArray($csvFile); return $csvData; }
/** * Convert csv data to array * @param string $csvFile * @param string|null $header * @param array $data * @return array */ public function convertCsvToArray(string $csvFile, string $header = null, array $data = []) { $csvData = str_getcsv($csvFile, "\n"); foreach ($csvData as $row) { $row = str_getcsv($row, ","); if (!$header) { $header = $row; continue; } $data[] = array_combine($header, $row); } return $data; }
array:6 [ 0 => array:6 [ "Serial #" => "11122203" "Asset Tag #" => "" "Hardware Standard" => "" "Tech Spec" => "2.4GHz/5GHz" "Current Status" => "test "Date Modified" => "08/16/2019" ] 1 => array:6 [ "Serial #" => "SERHPT12345" "Asset Tag #" => "" "Hardware Standard" => "Lenovo - Lenovo Tab 4.8+" "Tech Spec" => "A1395 / 32GB / Wi-Fi" "Current Status" => "test" "Date Modified" => "08/19/2019" ] 2 => array:6 [>] 3 => array:6 [>] 4 => array:6 [>] 5 => array:6 [>] ]
&amp;lt;?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Storage; class UploadController extends Controller { /** * Stores the file and convert to array * @param Request $request * @return dumped data */ public function store(Request $request) { $fileName = date("m-d-y-H-i-s") . '.csv'; $path = $request-&amp;gt;file('input_file')-&amp;gt;storeAs('uploads', $fileName); $items = $this-&amp;gt;processCsv($path); dd($items); } /** * Process Csv File and convert to an array * @param string $path * @return array */ public function processCsv(string $path) { if (!Storage::exists($path)) { return false; } $csvFile = Storage::get($path); $csvData = $this->convertCsvToArray($csvFile); return $csvData; } /** * Convert csv data to array * @param string $csvFile * @param string|null $header * @param array $data * @return array */ public function convertCsvToArray(string $csvFile, string $header = null, array $data = []) { $csvData = str_getcsv($csvFile, "\n"); foreach ($csvData as $row) { $row = str_getcsv($row, ","); if (!$header) { $header = $row; continue; } $data[] = array_combine($header, $row); } return $data; } } }

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
2Hats Logic Solutions

2Hats Logic Solutions

2Hats Logic is a technology service company provide customized web and mobile application development.