Neeraj1005's avatar

Image not showing ?

Image is uploaded into database properly but it does not showing in my index file. plz, anyone can tell me why this is not showing an image. This is my output, click the link https://imgur.com/9nqp8hz

And my database fields are: https://imgur.com/3QmGr56 This is my controller

<?php

namespace App\Http\Controllers;

use App\mediaLibrary;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class MediaLibraryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $profiles = mediaLibrary::all();

        return view('mediaLibrary.index', compact('profiles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        request()->validate([
            'image' => 'required',
        ]);

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();
        Storage::disk('public')->put($image->getFilename().'.'.$extension,  File::get($image));

        $picture = new mediaLibrary();
        $picture->mime = $image->getClientMimeType();
        $picture->original_filename = $image->getClientOriginalName();
        $picture->filename = $image->getFilename().'.'.$extension;
        $picture->save();

        return redirect()->route('media.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\mediaLibrary  $mediaLibrary
     * @return \Illuminate\Http\Response
     */
    public function show(mediaLibrary $mediaLibrary)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\mediaLibrary  $mediaLibrary
     * @return \Illuminate\Http\Response
     */
    public function edit(mediaLibrary $mediaLibrary)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\mediaLibrary  $mediaLibrary
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, mediaLibrary $mediaLibrary)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\mediaLibrary  $mediaLibrary
     * @return \Illuminate\Http\Response
     */
    public function destroy(mediaLibrary $mediaLibrary)
    {
        //
    }
}

and this is my index file

    <section class="content">
        <div class="row">
            @foreach($profiles as $profile)
            <div class="card m-4" style="width: 18rem;">
                <img class="card-img-top" src="{{Storage::disk('public')->url('uploads/'.$profile->filename)}}" alt="{{$profile->filename}}">
                    <a href="#" class="btn btn-primary float-right">Delete</a>
                </div>
            </div>
            @endforeach     
        </div>
    </section>
0 likes
13 replies
Heimdall's avatar

Hello,

When you inspect your image on the browser, what is return ?

bugsysha's avatar

Change .env file where you have APP_URL to

APP_URL=http://localhost

or what ever is appropriate for your setup.

Also note if you are using php artisan serve that you would have to stop that process with CTRL+C and run it again.

Heimdall's avatar

idk, you dont have problem for other image ?

bugsysha's avatar

@neeraj1005 do you have right folder structure? I see you are referencing uploads in your blade files but I do not see you moving that file to uploads directory during upload. Are you sure that is the right path?

1 like
Neeraj1005's avatar

@bugsysha Now it's working I have change the img tag

<img class="card-img-top" src="{{asset('storage/app/public/'.$profile->filename)}}" alt="{{$profile->filename}}">

@bugsysha Now I want show original file name instead of this -> php7D80.tmp.jpg. How can I do this? can you plz tell me. should make some little changes if yes: plz tell me how can I achieve this..? Thank you In url http://localhost/supportcrm/storage/app/public/php7D80.tmp.jpg

1 like
Neeraj1005's avatar
Neeraj1005
OP
Best Answer
Level 9

@heimdall @bugsysha Thanks for your suggestion and giving time for this thread. :) This is the code for uploading image with original file name. And this is the url what I want http://localhost/supportcrm/storage/app/public/books.jpg

    public function store(Request $request)
    {
        request()->validate([
            'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();
        $originalname = $image->getClientOriginalName();
        $path = $image->storeAs('', $originalname);
        $mimetype = $image->getClientMimeType();

        $picture = new mediaLibrary();
        $picture->mime = $mimetype;
        $picture->original_filename = $originalname;
        $picture->filename = $path;   
        $picture->save();

        return redirect()->route('media.index');
    }

Blade img tag

<img class="card-img-top" src="{{url('storage/app/'.$profile->filename)}}" alt="{{$profile->original_filename}}">
Developer@publictrust.co.nz's avatar

Hi, I have a similar issues, in lavavel 7, the files are uploaded into the /storage/app/public dir (linked to /public/storage), which is the same level as JS and CSS dirs which DO show (200), but the images under storage won't show (404).

The browser url is http://localhost:3500/storage/cover_images/barak_1595464934.jpeg

Any ideas? I tried chmod permissions, everything seems right according to the tutorial and docs. Any other reason for this? I also checked if I copied the image manually into the js directory, it does show, so perhaps it's something different about the symlinked storage dir?

Neeraj1005's avatar

@scpworking have you run this command? php artisan storage:link and show me your blade image tag code

rafidAhsan's avatar

I have kind of the same problem

<{img} src="{{ asset('uploads/images/' . $product->image) }}" height="100px" width="100px" alt="$product->image">

This is not showing the picture but the out of {{ asset('uploads/images/'.$product->image) }} is

http://localhost:8000/uploads/images/"1599803302.JPG"

I don't know what I'm gonna do. Need help!

Snapey's avatar

@rafidahsan start your own question, there could be multiple reasons for your issue

1 like

Please or to participate in this conversation.