Laravel-medialibrary: The storage path is not part of the public path

Created on 31 Mar 2016  路  17Comments  路  Source: spatie/laravel-medialibrary

Hi,

I get the following error, using latest laravel.

UrlCouldNotBeDetermined in LocalUrlGenerator.php line 19:
The storage path is not part of the public path

Most helpful comment

I fixed issue by adding a url to the disk like below:

    'disks' => [    

        'media' => [
            'driver'    => 'local',
            'root'      => public_path('media'), 
            'url'       => env('APP_URL') . '/media',
        ],
],

All 17 comments

When configuring your disk in app/config/filesystems.php you should start your root with the public path. You can see an example of it in the readme.

So there is no way to upload to the storage folder, it always has to be public folder @freekmurze ?

I am foreseeing conflicts with this because we are using Forge, with envoyer. The problem that occurs is whenever we deploy a new version, the public folder gets sweeped.

I think you should be able to symlink the folder in storage to public.

https://laracasts.com/discuss/channels/general-discussion/handling-uploaded-local-files-with-envoy?page=1

Yes, thats correct. I have done that but the problem is, whenever I set:

    'media' => [
        'driver' => 'local',
        'root'   => public_path().'/media',
    ],

To:

    'media' => [
        'driver' => 'local',
        'root'   => storage_path('app').'/media',
    ],

It returns the error:

   [Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined]
   The storage path is not part of the public path

Your driver should stil have the public path link as root. Your code doesn't have to know it's actually in storage instead of public, the symlink will take care of that for you.

Im not sure what you mean, could you elaborate in steps?

This is what your driver should look like:

'media' => [
    'driver' => 'local',
    'root'   => public_path('media'),
],

Since your media is currently in the storage directory, you'll need to create a symlink to the public folder.

ln -s /path/to/storage/media /path/to/public/media

You'll probably need to set this up as some sort of post-deploy hook with Envoyer. I've never used the service myself so can't really help there.

Now the medialibrary will be able to access the storage folder via a path in the public folder.

I'm facing this exact same problem, it looks like I've done everything correctly, but I still get the same exception. What for me seems to be a problem is this method:

// LocalUrlGenerator.php
/*
 * Get the path where the whole medialibrary is stored.
 */
protected function getStoragePath() : string
{
    $diskRootPath = $this->config->get('filesystems.disks.'.$this->media->disk.'.root');

    return realpath($diskRootPath);
}

The first line returns the correct path /home/vagrant/website/public/storage/media.
But the call to realpath() turns the path into /home/vagrant/website/storage/app/public/media.

What am I doing wrong?

I'm having this issue whereby my public/media is symlinked to storage/media (ln -s ../storage/media public/media from project root) but the realpath() function expands the symlink.

The solution is to remove the realpath() from the return in LocalUrlGenerator::getStoragePath().

@WolfieZero that has fixed the problem thanks, but should it be added to the repo?

I fixed issue by adding a url to the disk like below:

    'disks' => [    

        'media' => [
            'driver'    => 'local',
            'root'      => public_path('media'), 
            'url'       => env('APP_URL') . '/media',
        ],
],

I fixed this issue by modified the following code
'disks' => [ 'local-media' => [ 'driver' => 'local', 'root' => public_path().'/img/posts', 'url' => env('APP_URL') . '/media', ], ]
Cause,public_path() is define the public path to public , just add '/img/posts' to public.

I am having the same problem. The method above with adding 'url' help with the error, but now my media gets wiped out every time i deploy to aws elastic beanstalk.
I am not too sure how the symlink works.
i do not have a storage/media path.
the downloads go to public/media, and it is ignored by gitignore.

Be careful and understand what disk you are using. Local does not mean to be public. Public is for real public resources. (According to the default disk from a fresh Laravel installation)

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

I am still having some issues with the media library. Can some who is willing to assist with the public path.
Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\UnreachableUrl [49;22m : [33mUrl C:\laragon\www\Real-Estate-Application\public\/images/locations/birmingham.jpg cannot be
reached[39m

code
'disks' => [

    'local' => [
        'media' => [
            'driver' => 'local',
            'root'   => public_path('images/locations'),
        ],
        //'driver' => 'local',
        //'root' => storage_path('app'),
        //'root'      => public_path('public/images/locations')


    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

=========================

public function run()
{
$locations = [
'London',
'Manchester',
'Liverpool',
'Birmingham',
'Reading',
];

    foreach ($locations as $location){
        $slug = Str::slug($location);
        $locationObject = Location::create([
            'name' => $location,
            'slug' =>  $slug
        ]);

       $locationObject->addMediaFromUrl(public_path('/images/locations/' . $slug . '.jpg' ))
        ->toMediaCollection('photo');

@chisumo2016 I think you should use addMedia only instead of addMediaFromUrl

I'm not checking the code but I remember addMediaFromUrl is mean to be used for remote URLs via http

Docs: https://docs.spatie.be/laravel-medialibrary/v7/api/adding-files/#addmedia

Was this page helpful?
0 / 5 - 0 ratings