2

My question is about the problem in link below :

Understanding file storage and protecting contents Laravel 5

I need to use the same method mentioned in above example but instead of an image I need to provide a download link for PDF file or a link to open PDF file in browser and I can't do that because, as mentioned in above example's comments The Storage::disk('private')->get($file) returns the CONTENT of the file NOT the URL.

Please tell me how can I convert the row data (content of file) to a file and provide the link for the users inside the view.

2 Answers2

3

You should follow the below steps:

I have store pdf file into storage/app/pdf

In controller:

public function __construct()
{
    $this->middleware('auth');
}

public function index(Request $request, $file)
{   

    $file = storage_path('app/pdf/') . $file . '.pdf';

    if (file_exists($file)) {

        $headers = [
            'Content-Type' => 'application/pdf'
        ];

        return response()->file($file, $headers);
    } else {
        abort(404, 'File not found!');
    }        
}

if laravel below 5.2: Add use Response; above controller class in the controller.

public function index(Request $request, $file)
{   

    $file = storage_path('app/pdf/') . $file . '.pdf';

    return Response::make(file_get_contents($file), 200, [ 'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="'.$file.'"'

    ]);       
}

In web.php

Route::get('/preview-pdf/{file}', 'Yourcontroller@index');

In the blade view:

<a href="{{ URL('/preview-pdf/'.$file )}}" target="_blank">PDf</a>
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24
0

According to the Laravel documentation you can simply use the download method on the Storage facade.

From your controller, return the result of the command.

return Storage::disk('private')->download($file);

tamrat
  • 1,815
  • 13
  • 19
  • Thank you so much for your help. It worked exactly as I expected. – kianoosh Azarvandi Feb 21 '20 at 09:39
  • one more question, how can I open the file in browser instead of downloading it ? – kianoosh Azarvandi Feb 21 '20 at 09:40
  • @kianooshAzarvandi You need to get URL and then add this URL in an anchor tag with the target `blank`. I have updated my answer. – Amit Senjaliya Feb 21 '20 at 09:46
  • @kianooshAzarvandi I'm not entirely sure if Laravel 5.1 supports File Responses but you can try to `return response()->file($pathToFile)`. If this doesn't work try `return response()->download($pathToFile, $name, ['Content-Type' => 'application/pdf', 'Content-Disposition' => "inline; filename=\"{$name}\""]);` What you are doing here is modifying the Content-Disposition header, so that the browser will display the file rather than try to download it. – tamrat Feb 21 '20 at 09:55
  • Undefined variable: pathToFile – kianoosh Azarvandi Feb 21 '20 at 10:28
  • The file is being served through `Storage::disk('private')` and the path is inside private folder which is not accessible without serving it using `Storage::disk('private')` – kianoosh Azarvandi Feb 21 '20 at 10:33