Can't write image data to path (Laravel)
I'm not able to save anything to the storage directory in a Laravel project and receive the following error:

I've spent the last three weeks trying to find out why I can't save images to the Storage folder in Laravel with no luck. I've scoured StackOverflow and have come to the conclusion that it's probably due to my Docker image. I'd love to use my custom images if possible but get them working. Here's my setup:
Dockerfile (base image)
ARG VERSION=7.4
FROM php:${VERSION}-fpm-alpine
# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS"
# Install zip for csv stuff
# hadolint ignore=DL3018
RUN apk add --no-cache \
libzip-dev \
zip \
&& docker-php-ext-install zip \
&& apk del libzip-dev
# Install gd for image stuff
# hadolint ignore=DL3018
RUN apk add --no-cache libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev \
&& docker-php-ext-install gd \
&& apk del libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
# Install Nginx & PHP packages and extensions
# hadolint ignore=DL3018
RUN apk add --no-cache \
# for PHP/Laravel
git \
icu-dev \
msmtp \
nginx \
unzip \
# zip \
&& mkdir -p /run/nginx \
&& docker-php-ext-install \
pdo_mysql \
opcache \
&& { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/php-opocache-cfg.ini \
&& apk del icu-dev
COPY /config/nginx.conf /etc/nginx/conf.d/default.conf
COPY /config/msmtprc /etc/msmtprc
COPY /scripts/start.sh /etc/start.sh
COPY --chown=www-data:www-data src/ /var/www/html
WORKDIR /var/www/html
EXPOSE 80 443
ENTRYPOINT ["/etc/start.sh"]
Dockerfile (project, references the Dockerfile above ^)
FROM justintime50/nginx-php:dev # the dockerfile above
COPY --chown=www-data:www-data ./src /var/www/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
RUN php composer.phar install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist \
&& chmod -R 775 storage \
&& php artisan storage:link \
&& chmod -R 775 bootstrap/cache
Docker Compose
version: "3.7"
services:
laraview:
build: .
restart: always
container_name: laraview
volumes:
- ./src/storage:/var/www/html/storage
networks:
- traefik
- laraview
labels:
- traefik.enable=true
- traefik.docker.network=traefik
- traefik.frontend.rule=Host:laraview.localhost
- traefik.port=80
env_file:
- init-db.env
depends_on:
- laraview-db
laraview-db:
image: mysql:8.0.18
restart: always
container_name: laraview-db
env_file:
- init-db.env
volumes:
- ./db:/var/lib/mysql
networks:
- laraview
ports:
- "3306:3306"
labels:
- traefik.enable=false
networks:
traefik:
external:
name: traefik
laraview:
name: laraview
Upload Image File (Laravel)
public function updateProfilePic(Request $request)
{
$request->validate([
'upload_profile_pic' => 'required|image|mimes:jpeg,jpg,png|max:2048',
]);
$id = request()->get('id');
$file = $request->file('upload_profile_pic');
$img = Image::make($file)
->resize(320, 240)
->save(storage_path(), $file->getClientOriginalName());
// Upload Profil Pic (IMAGE INTERVENTION - LARAVEL)
//Image::make($request->file('upload_profile_pic'))->fit(150, 150)->save(storage_path('avatars/'.$id.'.png'));
session()->flash("message", "Profile picture updated successfully.");
return redirect()->back();
}
References:
- Nginx/PHP image: https://github.com/Justintime50/nginx-php-docker/tree/develop
- Laravel project: https://github.com/Justintime50/laraview/tree/develop

所有评论(0)