DLUK44
1 month ago

File Uploads on Windows Server

Posted 1 month ago by DLUK44

Hi. I have a form that allows a user to upload an image. When a user submits the form, I store it using

$path = $request->file('image')->store('UploadedFiles')

In Windows, I can see the file is in

storage\app\UploadedFiles 

with a hashed name, and $path contains something like

UploadedFiles/AXF8JV8as4tdoOubZsVNnwAYvoZaZ73lfAPVf7jP.png

Something seems to have already gone wrong here as the Laravel documentation says "The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public." so why has my file ended up in

storage\app\UploadedFiles 

My question is, how do I display that image to the user as soon as they have submitted the form?

One way would be to copy or move the file to the public directory and then just use.

<img src='AXF8JV8as4tdoOubZsVNnwAYvoZaZ73lfAPVf7jP.png'>

but is this the correct or best way to do this?

I have read about

php artisan storage:link

and this seems to create a storage directory under /public. If I then get a directory listing of public it shows the newly created storage directory with a 'l' attribute to show that it is a link, so this process does seem to be working.

d----l       07/08/2020     12:35                storage

It is empty apart from a .gitignore file. I am confused about what this is meant to do, especially on Windows.

If I submit the form again after creating the storage:link, it doesn't seem to change the behaviour. The file still goes to

storage\app\UploadedFiles 

and the

public/storage

is empty.

If I store the file using

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

goes in to

storage\app\public

but it now also appears in

public\storage

I assume because of me running

php artisan storage:link

$path contains

public/cLjF6cyx6fzBzzltTFNI827KjGKUTMMNIPsd5QMB.png

I can now access the file from the browser by going to

<img src='storage/cLjF6cyx6fzBzzltTFNI827KjGKUTMMNIPsd5QMB.png'

but I am not sure I like that URL and this doesn't seem right to put every upload in the same folder.

So, should I just copy the file to /public each time or should I just always use store('public') and access it with the /storage/ URL or am I missing some Laravel magic that I should be taking advantage of?

Thanks. David.

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