VIEW PDF 404 LARAVEL 7

Viewed 129

I want to have a button where a client can view an applicant's document that the candidate has uploaded, but for some reason I cannot figure out why it doesn't work.

It was working fine offline, but I deployed the site via FTP today and It doesn't want to work.

I am less than a junior developer so please be kind about messy code,

Here is my code for

upload blade:

 <form action="{{route('cv')}}" method="POST" enctype="multipart/form-data">@csrf
 <div class="card">        
 <div class="card-header">Update CV</div>
 <div class="card-body">
 <input type="file" class="form-control" name="cv"><br>
  <button class="btn btn-success float-right"
  type="submit">Update</button>
  @if($errors->has('cv'))
   <div class="error" style="color: red;">{{$errors->first('cv')}}</div>
                             @endif 
                            </div>
                            </div>

Code for route:

Route::post('user/cv','UserController@cv')->name('cv');

Code for Controller:

public function cv(Request $request){
    $user_id = auth()->user()->id;
    $cv = $request->file('cv')->store(public_path('storage/files'));
    Profile::where('user_id',$user_id)->update(['cv'=>$request->file('cv')->getClientOriginalName()]);
    
    return redirect()->back()->with('message', 'CV Successfully Updated!');
 }

Code for viewing the CV :

<a href="{{Storage::url($profile->cv)}}" class="btn btn-outline-primary">View my CV</a>

When I click the button I get this url:

cvroad.co.za/storage/Kevin%20Breed.pdf

The name of the file is : Kevin Breed.pdf and that is how it is seen in my database aswell.

but still 404 not found?

Any and all help would be appreciated, thank you!

1 Answers

Instead of hard-coding the file path, use Laravel's storage_path() to get to the right path.

$cv = $request->file('cv')->store(storage_path('files'));

Though if you're using Storage::url(), then it's looking in public/storage, so the proper way to go about it might to use the public_path

$cv = $request->file('cv')->store(public_path('storage/files'));

Also when saving to the database, you're saving the full path, not just the name. You can store just the name like this:

Profile::where('user_id',$user_id)->update(['cv'=>$request->file('cv')->getClientOriginalName()]);
Related