ホーム > Laravel > How to Resolve file_put_contents failed to open stream Permission denied Error【Laravel】
Laravel

How to Resolve file_put_contents failed to open stream Permission denied Error【Laravel】

Thank you for your continued support.
This article contains advertisements that help fund our operations.

This article outlines a solution for the file_put_contents failed to open stream Permission denied error that may occur when deploying in Laravel or setting up an environment in Docker.

Error Message

file_put_contents(/var/www/html/laravel-docker/src/storage/framework/views/b560c9abc12f714c866236b477e2d6a6.php): Failed to open stream: Permission denied

Solution

Grant permissions to the storage directory to resolve this issue.

chmod -R 777 storage

Explanation of the Solution

This error occurs because the system does not have permission to read and write files under the storage directory. By granting the necessary permissions to the storage directory, the issue can be resolved.

chmod -R 777 storage

That's it!

Further Details

What is storage?

When you install a Laravel project, the following directory structure is created:

├── app
│   ├── Console
│   ├── Exceptions
│   ├── Http
│   │   ├── Controllers
│   │   ├── Middleware
│   ├── Models
│   ├── Providers
├── bootstrap
│   ├── cache
├── config
├── database
│   ├── factories
│   ├── migrations
│   ├── seeders
├── public
│   ├── index.php
├── resources
├── routes
├── storage ← this one
│   ├── app
│   ├── framework
│   │   ├── cache
│   │   ├── sessions
│   │   ├── testing
│   │   └── views
│   ├── logs
├── tests
├── vendor
├── .env
├── .env.example
...etc.

Inside this structure, there is a storage directory. This directory serves several functions, such as:

  1. Outputting logs
  2. Storing various cache data
  3. Loading cached data

These roles contribute to rendering the display on the screen, and without proper permissions, the error mentioned earlier will occur.

Be Careful with the Directory Path

A common mistake is to misidentify the directory path. As mentioned, you must grant permissions to the storage directory, so be cautious with specifying the relative path.

chmod -R 777 relative/path

If needed, change directories to the Laravel project folder before running the chmod command:

cd laravel-project

I hope this information is helpful to someone.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!