PetroGromovo's avatar

Why image uploaded with spatie-laravel-media-library-plugin not found under storage?

On laravel 10 / filamentphp 3 site I have added spatie-laravel-media-library-plugin and upoading file with comnponent :

SpatieMediaLibraryFileUpload::make('image')
    ->disk('local')
    ->preserveFilenames(),

I see row in media table and file is uploaded under /ProjectRoot/storage/app/app_media/5/ subdirectory, but in browser I see that 404 error is returned on

http://local-host.com/storage/app_media/5/wizdom.jpg

url

I found valid file under

file:///ProjectRoot/storage/app/app_media/5/wizdom.jpg

path

but

/ProjectRoot/public/storage/

subdirectory is empty.

I run several times commands

cd public
rm storage
cd ../
php artisan storage:link

INFO  The [public/storage] link has been connected to [storage/app/public].

With output above that link was created with success.

How that can be fixed ?

"laravel/framework": "^10.35.0",
"filament/filament": "^3.0-stable",
"filament/spatie-laravel-media-library-plugin": "^3.1",
"spatie/laravel-medialibrary": "^10.15.0",

Thanks in advance!

0 likes
8 replies
jlrdw's avatar

Does the image display correctly.

wblondel's avatar

Are you using Apache? For it to follow symlinks you have to set Options +FollowSymLinks for the directories concerned.

PetroGromovo's avatar

@wblondel Yes Apache/2.4.52 (Ubuntu)

I check other laravel applications with storage used and see working sym links in public/storage subdirectory

In virtual conf file I have

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /ProjectRoot/public
    ServerName local-host.com
    ServerAlias local-host.com

    <Directory /ProjectRoot/public>
    AllowOverride All
    Order Deny,Allow
    Allow from all
    Require all granted
    </Directory>

    Options FollowSymLinks
    DirectoryIndex index.php

    ErrorLog /ProjectRoot/storage/logs/error.log
    CustomLog /ProjectRoot/storage/logs/access.log combined
</VirtualHost>

What I have under public directory:

hoster@hoster-laptop:/ProjectRoot/public$ ls -la
total 35
drwxrwxrwx 1 root root 4096 гру 22 14:02 .
drwxrwxrwx 1 root root 8192 гру 22 14:00 ..
drwxrwxrwx 1 root root    0 гру 22 13:45 build
drwxrwxrwx 1 root root 4096 гру 16 16:42 css
-rwxrwxrwx 1 root root    0 жов 12 09:39 favicon.ico
drwxrwxrwx 1 root root    0 жов 19 13:27 fonts
-rwxrwxrwx 1 root root   21 гру 22 13:46 hot
-rwxrwxrwx 1 root root  603 жов 12 09:39 .htaccess
drwxrwxrwx 1 root root 4096 лис 25 07:15 images
-rwxrwxrwx 1 root root 1710 жов 12 09:39 index.php
drwxrwxrwx 1 root root 4096 лис 15 07:16 js
-rwxrwxrwx 1 root root   24 жов 12 09:39 robots.txt
lrwxrwxrwx 1 root root   54 гру 22 14:02 storage -> /ProjectRoot/storage/app/public
drwxrwxrwx 1 root root    0 гру 12 14:32 vendor
drwxrwxrwx 1 root root 4096 жов 12 09:39 webfonts

What else have I to check ?

ramonrietdijk's avatar
Level 30

I don't have experience with the media library but I think I see why it's not working. The symbolic link that you have created points to storage/app/public. However, the files are not stored there. They're located in storage/app/app_media, which is not accessible via the browser. This is likely what's causing the 404 errors.

You could change the disk driver to public to solve this as the files will then be saved in storage/app/public/app_media/.... Note that everyone can access these files.

SpatieMediaLibraryFileUpload::make('image')
    ->disk('public')
    ->preserveFilenames(),
1 like

Please or to participate in this conversation.