andre0ani's avatar

Pbm with a file upload tuto

Hello,

I've done this tuto for upload file : https://www.5balloons.info/example-file-upload-validation-laravel-5-6/#comment-2600

But I have a error after validate the form, the uploadfile route is not defined. I don't understand why...

And if I try to make a link like this Fichiers I have this : Route [uploadfile] not defined But if I put upoadfile in the adress bar, it's working.

Any idea please ? Thanks ;-)

ANDRE Ani

0 likes
17 replies
ahmeddabak's avatar

the route not defined error usually means you didn't define the route in the web.php file.

make sure to add ->name('uploadfile');to the end of the route definition.

Snapey's avatar

Show the complete error message?

We cannot see your link above. If you want to put code inline you need to put ` before and after the code

Cronix's avatar

Where in the tutorial does it show to use route('uploadfile')? I don't see that anywhere (either did search on the page).

To use route('name-of-route'), you need to have a route with the name of name-of-route defined, like

Route::post('uploadfile','HomeController@uploadFilePost')->name('name-of-route');

Then you can use it like

<form action="{{ route('name-of-route') }}">
andre0ani's avatar

Thanks. For make a link, it's working now.

But always the error The requested URL /uploadfile was not found on this server. for the upload.

Here's the code :

web.php :

Route::get('uploadfile', 'Controller@uploadfile')->name('uploadfile');
Route::post('uploadfile','Controller@uploadFilePost')->name('uploadfile');

Controller :

public function uploadFile(){
      return view('uploadfile');
    }


public function uploadFilePost(Request $request){
      $request->validate([
          'fileToUpload' => 'required|file|max:1024',
      ]);

      $fileName = "fileName".time().'.'.request()->fileToUpload->getClientOriginalExtension();

      $request->fileToUpload->storeAs('logos',$fileName);

      return back()
          ->with('success','You have successfully upload image.');
}

And the form :

<form action="/uploadfile" method="post" enctype="multipart/form-data">
       @csrf
        <input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
        Transférer le fichier <input type="file" name="fileToUpload" id="exampleInputFile" accept="application/msword, application/pdf, image/*" title="Uniquement des images, .doc ou .pdf" />
        <input type="submit" class="btn btn-primary" value="Envoyer" />
      </form>

The error is surely on my code and not in the tuto. Thanks ;-)

Snapey's avatar

Your route is /uploadfile, ie from the root of the website. This will only work if you have correctly setup the site. If you have /public/ or something else in your URLs then this will not work

You cannot have two routes with the same name however, this appears not to matter as you are not using named routes

andre0ani's avatar

I've just followed the tutorial, and I have this error after validated the button for uploading the file : Class App\Http\Controllers\Request does not exist

The controller :


  public function uploadFile(){
      return view('uploadfile');
    }



  public function uploadFilePost(Request $request){
      $request->validate([
          'fileToUpload' => 'required|file|max:1024',
      ]);

      $fileName = "fileName".time().'.'.request()->fileToUpload->getClientOriginalExtension();

      $request->fileToUpload->storeAs('logos',$fileName);

      return back()
          ->with('success','You have successfully upload image.');

}

The web.php file :

Route::get('uploadfile', 'Controller@uploadfile')->name('uploadfile');
Route::post('uploadfile','Controller@uploadFilePost')->name('uploadfile');

And the form :

<form action="/uploadfile" method="post" enctype="multipart/form-data">
             @csrf
              <input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
              Transférer le fichier <input type="file" name="fileToUpload" id="exampleInputFile" accept="application/msword, application/pdf, image/*" title="Uniquement des images, .doc ou .pdf" />
              <input type="submit" class="btn btn-primary" value="Envoyer" />
            </form>
Snapey's avatar

So we've moved on? Its no longer a question about missing route?

And, as I mentioned, you cannot have two routes with the same name

change

Route::get('uploadfile', 'Controller@uploadfile')->name('uploadfile');
Route::post('uploadfile','Controller@uploadFilePost')->name('post.uploadfile');

in the form change

<form action="{{ route('post.uploadfile') }}" method="post" enctype="multipart/form-data">
             @csrf
              <input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
              Transférer le fichier <input type="file" name="fileToUpload" id="exampleInputFile" accept="application/msword, application/pdf, image/*" title="Uniquement des images, .doc ou .pdf" />
              <input type="submit" class="btn btn-primary" value="Envoyer" />
            </form>

and please make sure you validate to only accept file types you expect (in the controller, not in the form)

andre0ani's avatar

Ok, excuse me, I had 2 questions at the beginning...

I've just follow the tuto and this is the web.php of it :

Route::get('uploadfile','HomeController@uploadfile');
Route::post('uploadfile','HomeController@uploadFilePost');

I have to name just one of these routes if I want to add it in a menu, isn't it ? Perhaps the error is because I named the 2 routes...

Snapey's avatar

You don't have to name routes at all. Its just a convenience... but if you do name them, call them what you like as long as the names are unique

andre0ani's avatar

Ok for the routes. I named them to access with a menu.

I have this error for the upload : Class App\Http\Controllers\Request does not exist

Snapey's avatar
Snapey
Best Answer
Level 122

Import the Request class at the top of your controller

1 like
andre0ani's avatar

It's not done in the tuto, is it an error ?

Snapey's avatar

The tutorial also does not show you how to create a controller. Some basic understanding is required.

andre0ani's avatar

Ok, no error now ;-) Thanks a lot !

I don't have the successfully message (I surely forgot something else), but files are upload in storage/app/public/logos.

I've already done a little project in PHP and MVC model, but, with a framework, I'm lost. It's the beginning ;-) Thanks again !

chsnx's avatar

When you wrote return back()->with('success', 'status-message'); in your upload method. You are essentially redirecting back to the last page with success as the session variable.

You have to access success in your view using the session global method. You can do like this {{ session('success') }} to access the success from the session ..

It is mentioned in the docs that the with when used with back defines as session variable.

1 like
andre0ani's avatar

Thanks for the explanation for with and session. It's working ;-)

andre0ani's avatar

I opening a new discution, I'm trying to show the list of uploaded files.

Please or to participate in this conversation.