简体   繁体   中英

How to display an storage image Laravel 5.2

I want tho disply an image on my view, but i receive: File not found .

The two errors:

FileNotFoundException in FilesystemAdapter.php line 61: storage/app/public/covers/teste-bravo_cover.jpg

FileNotFoundException in Filesystem.php line 386: File not found at path: storage/app/public/covers/teste-bravo_cover.jpg

But the image is in the correct folder:

一只忙碌的猫文件夹上的文件

Well, in Laravel 5.2 storage:link won't work.

Image store on disk -works fine to store the image

if($cover){
            dump("ok1");
            Storage::disk('public_covers')->put($covername, File::get($cover));

            $coverObject = New Cover();      

            //path com o nome do arquivo
            $coverObject->imagePath = 'storage/app/public/covers/'.$covername;
            dump($coverObject->imagePath);
        }

        //Salva a capa com o path para o arquivo
        $coverObject->save();

My filesystems (with the "public_covers" disk) -works fine to store the image

        'public_covers' => [
            'driver' => 'local',
            'root' => storage_path('app/public/covers'),
            'visibility' => 'public',
        ],

View code: (if movie have cover, show them)

                @if($movie->cover)
                <img src="{{route('cover.image', [$movie->cover])}}" />
                @endif

Route code, directing to controller method

Route::get('/movieimage/{coverId}',
[
               'as' => 'cover.image',
               'uses' => 'MovieController@getimage'
           ]
);

Controller Method to take the image

    public function getimage($coverId){
        $movie = Cover::find($coverId);
        //dump($movie);
        $imagePath = $movie['imagePath'];

        dump($imagePath);

        $file = Storage::disk('public_covers')->get($imagePath);

        $response = Response::make($file, 200);
        return $response;     
    }

you can try

Need to include this

use Illuminate\Support\Facades\Storage;

And set in controller or route

Route::get('storage/{filename}', function ($filename)
{

    $files = Storage::files('logs');  // set repo name of storage folder

    $path = storage_path('public/' . $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;
});

I am new to laravel. I had the same problem though am on laravel 5.8. I had created the symlink to display a pdf as a view but the FileNotFoundException persisted. I ended up accessing the pdf using this url localhost:8000/storage/filename.ext .

After running ln -s /path/to/laravel/storage/public /path/to/laravel/public/storage or php artisan storage:link a storage folder is added to your appname/public directory. I think that once the link is created we access the images in the storage through the public link created. Try accessing the images via the new /appname/public/storage/app/pathtoimage folder created in the public directory and not pointing directly to the /appname/storage folder

<img src={{asset('/storage/pathtoimg')}} /> # using storage folder in public

Check this solution will work for you

How I solved my problem:

First of all, save the files on "public" folder.

On my filesystems.php i change my disk. Now my root are "public_path" and not "storage_path"

        'public_covers' => [
            'driver' => 'local',
            'root' => public_path('images/covers'),
            'visibility' => 'public',
        ],

My controller method

    public function store(Request $request){

        $cover = $request->file('cover');
        $inputs = $this->request->all();

        $this->validate($this->request, [
            'title' => 'required|max:255',
            'description' => 'required',
            'gender' => 'required',
            'secondGender' => 'required',
            'year' => 'numeric|min:1900|max:2100',
            'lenght' => 'numeric|min:10'
        ]);

        //build movie model object
        $movie = New Movie();
        $movie->user_id = \Auth::user()->id;
        $movie->gender_id = $inputs['gender'];
        $movie->secondgender_id = $inputs['secondGender'];
        $movie->title = $inputs['title'];
        $movie->slug = str_slug($inputs['title']);
        $movie->description = $inputs['description'];
        $movie->lenght = $inputs['lenght'];
        $movie->year = $inputs['year'];

        //TODO Mudar a composição do nome da foto para impedir que no futuro se sobreponha uma capa com outro filme que venha a ter o mesmo nome
        //TODO: Change the name createan to avoid overwrite the cover with an movie with the same name
        $covername = str_slug($inputs['title']).'_cover' .'.jpg';

        //if have an cover file
        if($cover){
            //storage the file on my public_covers (look my filesystems.php)
            Storage::disk('public_covers')->put($covername, File::get($cover));
            $coverObject = New Cover();      

            //path com o nome do arquivo
            //path with the file name
            $coverObject->imagePath = 'images/covers/'.$covername;

            $coverObject->save();
            $movie->cover_id = $coverObject->id;
        }

        $movie->save();

        return redirect()
            ->route('user.dashboard')
            ->with([
                       'success' => 'Filme adicionado com sucesso!',
                   ]);
    }

My view image code

@if($movie->cover)
                <img class="cover" src="{{ asset($movie->cover->imagePath) }}" alt="{{ $movie->title }}"">
                @else
                <img class="cover" src="{{ asset('images/noimage.jpg') }}" alt="{{ $movie->title }}">
                @endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM