0

Controller

public function uploadFile()
{
      if(isset($_POST["uploadFile"])){
        $filePath = realpath($_FILES["file"]["tmp_name"]); //GETTING THE FULL PATH OF THE FILE WHICH WANTS TO BE SAVED INTO THE DB
        $arr = [];
            if (($handle = fopen($filePath, 'r')) !== FALSE){
                $i = 0;
                while (($lineArray = fgetcsv($handle, 4000)) !== FALSE){
                    for ($j=0; $j<count($lineArray); $j++){
                        $arr[$i][$j] = $lineArray[$j];
                    } $i++;
                } fclose($handle);
            }
        $csv = array_slice($arr,1);
        foreach($csv as $line){
          mysqli_query($mysqli, "INSERT INTO dialog(`eType`,`eVal`,`intent`,`reply`) VALUES('$line[0]','$line[1]','$line[2]','$line[3]')"); //FOR EACH LINE SAVE INTO THE DB
        }
      }
}

Route

Route::get('uploadFile', 'Chatbot\ChatbotController@uploadFile');

View

<form method="post" action="{{action('AltHr\Chatbot\ChatbotController@uploadFile')}}" enctype="multipart/form-data">
      <input type="file" name="file"> <br>
      <button type="submit" name="uploadFile" class="btn alt-btn-black btn-xs alt-btn">UPLOAD FILE</button>
</form>

So, Its my first day learning laravel, can somebody tell me why am I having this errror MethodNotAllowedHttpException in RouteCollection.php

I am trying to upload a CSV file in the form to the database. what am I doing wrong here ?

9
  • 3
    for post request you have to use Route::post routing method, instead of Route::get Commented Jul 19, 2017 at 5:47
  • Guys, now im having this error "TokenMismatchException in VerifyCsrfToken.php" Commented Jul 19, 2017 at 6:00
  • please clear cache, php artisan cache:clear Commented Jul 19, 2017 at 6:00
  • 1
    <input type="hidden" name="_token" id="csrf-token" /> add it in your form Commented Jul 19, 2017 at 6:04
  • 1
    Laravel uses csrf-token to prevent CSRF Commented Jul 19, 2017 at 6:07

2 Answers 2

3

Your route is wrong. Upload is POST request so:

Route::post('uploadFile', 'Chatbot\ChatbotController@uploadFile');
Sign up to request clarification or add additional context in comments.

Comments

-1

The real question is : Why aren't you using Eloquent or atleast the QueryBuilder to insert in the DB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.