I have created a folder named uploads
which is on same level as of app
folder. How can I use a Laravel helper like URL::to
to get the file path in it?
Whilst the comments above are true (the files are not directly accessible by the browser) - you can still provide link access to these files through a function.
The trick is to use php function readfile()
This allows you to then secure the files, and apply permissions to them - i.e. only certain users can access the files, users can only access their own files etc.
This is an example of one way you can allow a logged in user to access a file
A route like this:
Route::get('/view/{$file}', ['as' => 'viewfile', function($file) {
// Ensure no funny business names to prevent directory transversal etc.
$file = str_replace ('..', '', $file);
$file = str_replace ('/', '', $file);
// now do the logic to check user is logged in
if (Auth::check())
{
// Serve file via readfile() - we hard code the user_ID - so they
// can only get to their own files
readfile('../uploads/'.Auth::user()->id.'/'.$file);
}
}]);
Then in a view file somewhere you can link to the file like this:
<p>Here is a link to your file: {{ URL::route('viewfile', ['your_file_name.jpg']) }}</p>