Home > Software engineering >  How do I rename a pdf, upload it and open it in another browser tap?
How do I rename a pdf, upload it and open it in another browser tap?

Time:01-18

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 App\Http\Controllers;
use Illuminate\Http\Request;use Illuminate\Support\Facades\Storage;use Illuminate\Routing\Controller;
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);
}
}

CodePudding user response:

If you use your storage path then run arisan command

php artisan storage:link 

CodePudding user response:

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');
}
  • Related