skip to Main Content

I have a project in laravel where I have to take an pdf file, rename it and saves it in the storage. It should then redirect back. With another function it shoud show this pdf file in the browser in another tap.

I am not getting an error but it is not saving my file

**This is the controller:**

namespace AppHttpControllers;
use IlluminateHttpRequest;use IlluminateSupportFacadesStorage;use IlluminateRoutingController;
class FileController extends Controller
{
public function index()
{
    return view('file-upload');
}
public function upload(Request $request)
{
    $request->validate([
        'file' => 'required|mimes:pdf|max:2048',
    ]);

    $file = $request->file('pdf');

    if (Storage::exists('public/test.pdf')) {
        Storage::disk('public')->delete('test.pdf');

        $file->storeAs('public', 'test.pdf');
    } else {
        $file->storeAs('public', 'test.pdf');
    }

    return redirect()->back();
}

public function show()
{
    $path = public_path('storage/test.pdf');

    return response()->file($path);
}
}

2

Answers


  1. Chosen as BEST ANSWER

    I changed file in the validation to pdf and redirected the show function to the <iframe> view and everything works well now.

    $request->validate([
      file' => 'required|mimes:pdf|max:2048',
    ]);
    

    And the show function:

    public function show()
    {    
        return view('file.show');
    }
    

  2. If you use your storage path then run arisan command

    php artisan storage:link 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search