Search code examples
laravellaravel-5file-uploadlaravel-5.8document-view

Show files in browser that are not in web directory and not linkable


We are using Laravel v.5.8 and have our public files in a directory like...

/www/public/pubfiles/myfile.php

I can link to these with no problem. We also have an upload script that by design puts the files in...

/uploads/nonlinkablefile

And then stores the path and a filename, etc. in a db. When someone clicks a link to retrieve these files, a script gets the file and returns it as a download.

The files in the second option are stored this way so they cannot be linked to or shared. We are able to check a users permission on pages or directly on a file to see if they should be able to download.

What I'm wondering now is, if we can do those checks, and then somehow display the files (PDFs moslty) in a new tab in the browser vs. forcing a download.

Since there isn't a web-enable path to the file, I'm not finding a way to do so.

Thanks.


Solution

  • You can try with the file() function, instead of a download ask the browser to open the file, i.e.:

    return response()->file('/uploads/nonlinkablefile.pdf');
    

    You can find more info in the official documentation.

    Obviously the path in which the files are stored should be accessible by the webserver, but the file does not need to be under the public or DocumentRoot directory.