php - Incorrect image path for storage folder : Laravel 5.2.31
Get the solution ↓↓↓I have following image tag in blade.
{!! Form::image('/MySplash/1/file.png', $alt="Photo", null) !!}
Below is my code in Route file..
Route::get('MySplash/{IconID}/{UniqueFileName}', function ($IconID, $filename)
{
$path = storage_path() . '/SplashScreenIcons/' . $IconID . '/' . $filename;
if(!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;;
});
Finally I get the image url like below.
http://localhost/SportsApp/public/MySplash/1/file.png
What's the problem
- It does not go into route.
- Url is showing public. whereas the image folder is in storage directory.
Am I missing anything?
Answer
Solution:
Form::image()
creates a HTML input that uses a file resource. Rendering this input and the path to its image will not directly call yourGET
route. The framework assumes you provided a static path and will simply map it to thepublic
directory by default, for a browser to retrieve the resource.
You keep the icons in thestorage
, which means the files are not publicly available. If you're working in a Linux-based environment or you use Homestead locally, the simplest thing to do to make the icons available on client side is to create a symlink from yourpublic
folder tostorage
, like this:
ln -s /home/vagrant/Code/myproject/storage/SplashScreenIcons /home/vagrant/Code/myproject/public/MySplash
Then, you can use the Form helper as usual:
{!! Form::image('/MySplash/1/file.png', $alt="Photo", null) !!}
or use theasset()
helper:
<input src="{{'MySplash/1/file.png'}}" type="image">
One thing to keep in mind is that you'll need the symlink on your production as well, e.g. as a deployment hook.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: filter_sanitize_string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.