3

Almost always, when I am starting a Codeigniter 4 project, I am getting the following error:

"CodeIgniter\Cache\Exceptions\CacheException Cache unable to write to "/path/to/codeigniter-project/writable/cache/."

SYSTEMPATH/Cache/Handlers/FileHandler.php at line 61

which points at the below line:

throw CacheException::forUnableToWrite($this->path);

Cache Exception

What is the best way to solve this?

John Skoumbourdis
  • 3,041
  • 28
  • 34

4 Answers4

4

After doing a research, I've concluded into two options

Option 1: The very quick way to just solve the above issue is to go to the command tool (e.g. terminal) and change the permissions of the folder to be writable and also to change the owner of the folder to be the one that is used from your server application. For example, I am using Apache and the user for apache is www-data:

chmod -R 755 writable/ 
chown -R www-data:www-data writable/

or for macOS:

chmod -R 755 writable/ 
chown -R _www:_www writable/

Pros:

  • A quick copy-paste solution
  • Those commands - most of the times - works out of the box
  • Even if you don't have access to command tool, you can change the permissions and ownership of the folder through FTP (e.g. through FileZilla UI interface)

Cons:

  • You always need to add those commands into a new project
  • You may experience issues on development when you are trying to access the folder with your username

Option 2 (suggested for local development): Add the www-data or _www (for macOS) to be at the same group role as your user.

for Linux and Apache:

sudo usermod -aG www-data your_username

or for macOS and Apache:

sudo dseditgroup -o edit -a your_username -t user _www

If you are still getting the error also make sure that you have the folder as writable with the following command:

chmod -R 755 writable/ 

I don't know why but it may also need to do a restart of the Apache.

Pros:

  • It works for new projects with no extra effort
  • You don't need to change the owner of the folder so you can have access to the folder for development
  • Once this change is available to the server or locally, you don't need to use sudo anymore if the chmod command is required

Cons:

  • Not that safe for production environments (especially if you don't know what you are doing) since you would prefer to have specific users with specific roles rather than have users to have access everywhere
  • It requires more effort and it is not that straight forward for beginners
  • It needs more permissions to the server (e.g. sudo access)
John Skoumbourdis
  • 3,041
  • 28
  • 34
  • 1
    I don't know who suggested option 2 but I would be extremely careful with that. – parttimeturtle Aug 31 '22 at 21:18
  • References for the linux command: https://askubuntu.com/a/867170/173256 and https://serverfault.com/a/381707/407191 Reference for the macOS command: https://stackoverflow.com/a/5784745/2282575 – John Skoumbourdis Sep 01 '22 at 05:45
  • 2
    The danger is if you're grouping together the httpd user and a user that has privileges for things beyond what the httpd user should have; most accounts meant for actual, human users are going to fall into this category and god help you if you're running as root. I'd say option 1 is the safer choice in the eyes of 'least privilege'. – parttimeturtle Sep 01 '22 at 19:52
  • fair enough :) . I will update the response with [suggested for local development] and a comment at the cons. Thank you – John Skoumbourdis Sep 02 '22 at 04:09
  • If anyone reading this is using Xampp on an Ubuntu 22.04 local environment, you can check the Apache user by running the command ```ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'```. You'll then need to give that user the necessary privileges using chown like OP specified above, for example, on my system it was ```chown -R daemon:daemon folder_name/``` – kimetsu no yaiba Nov 22 '22 at 04:28
2

The OP has a good answer. Though sometimes you don't have access to the terminal or a secure shell (ssh), especially in a shared hosting environment to be able to run those commands.

I got a similar issue while working on a new project. I noticed that this normally happened because my application failed to write session files in the writable/session folder of my root project directory because of permission restrictions. I was using the CodeIgniter\Session\Handlers\FileHandler as my configured "session storage driver".

I managed to solve it by instructing the application to write these temporary files in the server's tmp directory.

This is by default in the /tmp directory.

Excerpt from php-src/php.ini-production

; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
;sys_temp_dir = "/tmp"

Solution

I opened my .env file residing at the root of my project directory and added the line below.

app.sessionSavePath = '/tmp/my_project_name/ci4_session'

If you don't have this .env file, create one starting with this default file content:

Notice the dot (.) in front of the filename.

CodeIgniter4 env file

Addendum

Similarly, in your case, the application fails to write cache files in the writable/cache folder of your root project directory because of permission restrictions. You most certainly are using file as your cache handler.

Solution

Open your .env file residing at the root of your project directory and add the line below.

cache.storePath = '/tmp/my_project_name/ci4_cache'

TIP(S)

These and more configurations can be found/modified in your .env file using the rules below:

  1. The configuration class file name, in lower case. I.e cache.
  2. Followed by a dot(.).
  3. Then the public configuration class's property name. I.e storePath.
  4. Steps 1,2,3 combined makes, cache.storePath, which becomes your .env file's key declaration. Then an equals sign =. And lastly, the .env file's value ('/tmp/my_project_name/ci4_cache') declaration. Finally coming up with, cache.storePath = '/tmp/my_project_name/ci4_cache'.

The configuration classes can be found in the app/Config folder of your root project directory. I.e:

app/Config/Cache.php
app/Config/App.php
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
1

I just create a folder inside "writable" folder with name "cache" and my project properly running.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '23 at 22:36
0

This worked for me, U should try this

Firstly cd into where your file is Example

cd /opt/lampp/htdocs/

This is where my file is

Secondly Write this command below in your command prompt

sudo chmod -R 777 file-name

Fortune
  • 181
  • 1
  • 5