jontyjago
1603
5
Laravel

Uploading files and storing path in database

Posted 2 years ago by jontyjago

I'm struggling with uploading files and then easily accessing them.

I've configured the storage link so anything placed in storage/app/public is visible through http://example.com/storage/ however this is where I'm having problems

The following code in my controller

$path = $request->file('docfile')->store('public');

        return $path;

saves the files to the correct directory but the $path returned is

public/Y9GvrBuY6RG2thIrvjXWsrLZYOPn2u9fLNW1v268.png

so if I save this to the database and try to link to that later the file will not be found, as the storage folder is linked, not the public folder.

If I don't put a folder name in the controller:

$path = $request->file('docfile')->store('');

        return $path;

then the $path variable only contains the file name but it gets saved to storage/app so is not found by http://example.com/storage/. Using 'storage' as the folder in the controller would save the file to storage/app/storage.

What I would like is to save just the file name in the database and for the file to be saved in the correct location so it is visible from http://example.com/storage/

Going forward I would like to be able to configure a custom file structure like http://example.com/name/uploads but I need to get the basics right first!

Please sign in or create an account to participate in this conversation.