Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chmod(): Operation not permitted in Laravel

Tags:

php

laravel

gd

chmod(): Operation not permitted while uploading images in laravel at localhost. Enabled GD library. Testing environment is with ubuntu 16.04 with Windows 10(WSL).

like image 826
Niyaz Avatar asked Dec 03 '22 19:12

Niyaz


2 Answers

I've just had the same problem. I've no idea why this happened, but in my case running composer dumpautoload fixed it for me.

like image 119
Inigo Avatar answered Dec 11 '22 16:12

Inigo


I'm working with Laravel 8, Docker Desktop 4.2.0 (hosting Ubuntu-20.04) and WSL2 on Windows 10.

I experienced the same error after I successfully cleaned up my Laravel cached files within my Docker container with

sail artisan optimize:clear

On re-serving the application and reloading it on my browser, it reported some Filesystem error. The trace log pointed to a line where Laravel was trying to execute the chmod() command and returned with the error

chmod(): Operation not permitted

I tried running

sail composer dumpautoload

within the my Docker container. The process ran for a while and then reported the same chmod(): Operation not permitted error on the command line.

This commands saved my day:

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    composer dumpautoload

Simply copy and paste them to command line where you run your sail commands and hit enter. After the last composer command completes, you can now a access your application without chmod() flagging an error.

WARNING: Please backup your modified Laravel files; especially, routes/web.php before running the above command.

UPDATE

Got to the point of uploading files via a form and chmod(): Operation not permitted popped up again. The issue may be due to changes in the directory structure/information inadvertently caused by installing multiple dependencies via composer.

Also, Docker containers and Docker images for Laravel applications are managed by Laravel Sail. Sail sets user access policy to the application when you run sail up. It does this by creating new users for the docker images and associating them in groups when the application starts. You can confirm this by running:

sail artisan sail:publish

and checking the Docker files in the generated docker folder in your application root. They are actually used by Sail to spin up new containers for your app.

SOLUTION

If you have anything close to my setup and you encounter the chmod(): Operation not permitted error; simply stop Sail:

sail stop

Wait for all containers to stop running and then bring it back up:

sail up
like image 45
Udo E. Avatar answered Dec 11 '22 15:12

Udo E.