2

I want to access the file storage/app/15573877669649.jpg.

Code to upload the file

$file = $request->file('cover_image');
$fileName = time().rand(111, 9999).".".$file->getClientOriginalExtension();
Storage::disk('local')->put($fileName, file_get_contents($file));

Code to retrieve the file in the blade.php file

<img src = "{{ URL::to('/').Storage::url($article->coverImage) }}" alt="image">

which returns

http://127.0.0.1:8000/storage/15573877669649.jpg

I have tried to manually put http://127.0.0.1:8000/storage/app/15573877669649.jpg but it's also not working

2

2 Answers 2

4
  • Open commandline inside your project
  • Type in php artisan storage:link

Your files will now be accessible under "/storage/file.jpg"

/ leads to public

/storage/ leads you to storage/app/public thanks to link

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this is already done, after doing all this I have raised the question. Do you think there is any issue in my code to get the file??
2

By default, Laravel's Storage::('local') is not accessible from public; the Storage::('public') is the one will be publish by command php artisan storage:link

So for your sample code, you can do it like this:

Uploading file

$file = $request->file('cover_image');
$fileName = time().rand(111, 9999).".".$file->getClientOriginalExtension();
Storage::disk('public')->put($fileName, file_get_contents($file));

In blade.php file

<img src = "{{ URL::to('/').Storage::disk('public')->url($article->coverImage) }}" alt="image">

// Or simpler
<img src = "{{ asset("/storage/$article->coverImage") }}" alt="image">

More detail of the 'disk' please check config file in /config/filesystems.php, and refer the the Public Disk section of official document


And, remember to enable the follow symlinks settings in your web server

// Apache:
<Directory>
    Options FollowSymLinks
</Directory>

// Nginx:
{
    disable_symlinks off;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.