0

So use is uploading a logo and it's path is stored in a database like this:

C:\xampp\htdocs\laravel\public\logo\1496912432.jpg

I am displaying the image like this:

<img class="images" id="image" src="{{$business->image}}" />

However I get this error:

Not allowed to load local resource: file:///C:/xampp/htdocs/laravel/public/logo/1496912432.jpg

How can this problem be solved?

//edit

Controller:

public function image(Request $request) {
    if($request->hasFile('img'))
    {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('logo/' . $filename);
        Image::make($image->getRealPath())->fit(303, 200)->save($path);
        $file = $request->file('img');
        $session = session()->get('key');
        $update_image = Business::find($session);
        $update_image->image = $path;
        $update_image->save();
        return ['url' => url('logo/' . $filename)];
    }
1
  • I guess "logo\1496912432.jpg" will be enough to store in the db and then you can use asset(your path) like hafiz said. Storing the full path may brake the code on a different environment. Commented Jun 8, 2017 at 9:13

3 Answers 3

2

Use Laravel file() to store files https://laravel.com/docs/5.4/requests#files

Store the $path to your db

$path = $request->photo->store('logo');

the $request->photo is depending on your input file attribute name. In your case, it should be $request->img.

the above code will create a folder (if not exist), namely "logo" and store to that folder with random string file name.

Also check your configuration for file, located at /config/filesystem.php. Default is set to public

Use asset function to get the full path from public folder

<img class="images" id="image" src="{{ asset($business->image }}" />
Sign up to request clarification or add additional context in comments.

2 Comments

have a look at my controller, how could I modify it to use the way you have suggested?
@Przemek change it to $request->img->store('logo') if your input file name is "img". Config file is at config/filesystem.php. The default is 'public' so it will store then and create the 'logo` folder
0

You can do in two ways

Best way is update url path when image saving save url path to db

$path = $request->photo->store('logo'); // in 5.4

The other way if you can't changes db url you can do some hack like this

$file = explode('/public/', $business->image);
echo asset($file[1]);

Comments

0

You want to store all files inside the web root. Because of cross-domain security, you cannot access the file:// domain/protocol from a http protcol. By using Laravel to store and retrieve, it will come from the same host.

1 Comment

cant be it. I can echo images in store on other places from the site. Just the difference in this instance is using vuejs.

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.