1
votes

I know this question has been posted several times, but I can't get it.

Now I use :

 // photo upload
 $dir = strtolower(str_random(2));
 $image = $request->file('image')->store('products/'. $dir);

It works perfectly, my file is uploaded to :

storage/app/products/ds/HASH.jpg

But it's not public, I saw the documentation, but I can't understand how to upload to public folder easily.

Can I use the store function ? I want the same behavior : the filename is hashed, the folder is created on the fly.

I tried :

Storage::setVisibility($image, 'public');

But it doesn't work (no error...).

The doc says to use :

Storage::put('file.jpg', $contents, 'public');

But how can I fill the $content variable ? How can I have the filename hashed easily ?

1
Have you run php artisan storage:link to create the public storage symlink?Jamesking56
Why not using Intervention? image.intervention.ioVipertecpro
Yes the link is created, but the link uses storage/app/public/ folder, now my images are in storage/app/ folderVincent Decaux

1 Answers

0
votes

I usually use the move function, something like this should work:

    $file = $request->file('file');

    $file_name = time() . $file->getClientOriginalName();                      

    $file_path = 'uploads/';

    $file->move($file_path, $file_name);

This would move your file to the public/uploads folder... instead of storage/app/public...

You can also access the storage files through something like this, but for this you'll have to add a route and method for file access

    return response()->file(storage_path(''));