Cannot write to directory using Faker image

Viewed 5760

I want to setup a test data through Faker using a factory. but when I try to add images to the model

        "images" => $faker->image('public/storage/images',400,300, null, false),

it returns an error

Cannot write to directory "public/storage/images"

even thought I ran

php artisan storage:link

How to solve this problem? I am using Ubuntu btw.

2 Answers

You are attempting to write to the directory public/storage/images, this is relative to the root of your server as in /public/storage/images.

You have to write to the storage directory of your app, such as:

$faker->image(storage_path('images'),400,300, null, false)

Or directly to the public folder as well:

$faker->image(public_path('images'),400,300, null, false)

Looks like the user-group or system cannot write to the folder. php artisan storage:link only creates a symlink to the public folder.

You need to update the permissions on the storage folder to allow creation of files. Check that the user group running the code has permission to write to the storage file and that the storage file has the proper CHMOD permissions

chmod -R 755 storage

Related