Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Spatie Media Library and permissions for the folders / files

Hello,

I have a problem with permissions for Spatie Media Library.

The application runs in a docker container.

I have defined the MEDIA_DISK=media with this configuration.

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

The library creates the main folder for the image without any problem. So I have : storage/app/media/6. Then it creates the original image without any problem, so I have : storage/app/media/6/image.jpg. Then it fails when executing mkdir() to create the conversions folder.

The 6 folder is created with 755 permissions.

To check is something would change, I have modified the permissions in the filesystem configuration file.

'media' => [
    'driver' => 'local',
    'root' => storage_path('app/media'),
    'url' => env('APP_URL').'/storage',
    'permissions' => [
        'file' => [
            'public' => 0655,
            'private' => 0655,
        ],
        'dir' => [
            'public' => 0777,
            'private' => 0777,
        ],
    ],
    'throw' => false,
],

But it doesn't work better, the mkdir() command still fails.

Does Spatie Media Library apply some specific permissions ? If yes, do you know if it's possible to configurate these permissions ? I don't have seen anything about folder / file permissions in the library documentation.

Do you have any idea why it fails ? nginx configuration ? laravel filesystem configuration ? docker configuration ? ... ?

Thanks a lot for your help.

V

0 likes
3 replies
tisuchi's avatar

@vincent15000 It seems the problem is neither the package nor the app. It's related to the permission inside the docker container.

Ensure that the user running the PHP application has ownership of the storage folder (typically www-data).

Maybe adjusting ownership could help you.

chown -R www-data:www-data /path/to/your/storage
1 like
vincent15000's avatar

@tisuchi Thank you for your help ... I will try this tomorrow and I come back to tell you if it works ;).

vincent15000's avatar

@tisuchi Not tested yet.

Here are some more informations : the demo application is running with docker on a VPS. I'm not the admin of the system, but just a user (vincent) with some rights and another user is the admin (mike)

  • the PHP user is set to appuser

Outside the docker container :

  • the owner of the application's folder' and files is *vincent with 775 / 664 permissions

  • when the Spatie Media Library creates a new folder and a new file, the owner is mike with 775 / 664 permissions => storage/app/media/4/image.jpg (4 and image.jpg created successfully), but storage/app/media/4/conversions (conversions folder creations failed)

Inside the docker container :

  • an unknown user with UID 1001 (seems to be the default docker user, but outside of the container, it's my UID --- vincent ---) has the full rights on the application's folders and files

  • appuser creates the images' folders and files

I have already this line in the nginx.dockerfile.

RUN chown -R ${NGINXUSER}:${NGINXGROUP} /var/www/html

But not in the php.dockerfile.

Here are my dockerfiles.

FROM nginx:stable-alpine

ENV NGINXUSER=appuser
ENV NGINXGROUP=appuser

RUN mkdir -p /var/www/html/public

ADD nginx/default.conf /etc/nginx/conf.d/default.conf

RUN sed -i 's/user www-data/user ${NGINXUSER}/g' /etc/nginx/nginx.conf

RUN adduser -g ${NGINXGROUP} -s /bin/sh -D ${NGINXUSER}

RUN chown -R ${NGINXUSER}:${NGINXGROUP} /var/www/html
# RUN chmod -R 755 /var/www/html/bootstrap/cache /var/www/html/storage
FROM php:8.3-fpm-alpine

ENV PHPUSER=appuser
ENV PHPGROUP=appuser

RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}

RUN sed -i 's/user = www-data/user = ${PHPUSER}/g' /usr/local/etc/php-fpm.d/www.conf
RUN sed -i 's/group = www-data/group = ${PHPGROUP}/g' /usr/local/etc/php-fpm.d/www.conf

RUN mkdir -p /var/www/html/public

ADD php/php-app.ini /usr/local/etc/php/conf.d/php-app.ini

RUN apk update && apk add --no-cache mariadb-client zip libzip-dev

RUN docker-php-ext-install pdo pdo_mysql zip

CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

Please or to participate in this conversation.