Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VertexBuffer's avatar

Faker Images "Cannot write to directory"

So I've seen a few other posts on here regarding this, but I've attempted all the best answers listed on those posts and none of them have worked for me, so I'm going to post here in the hopes that someone is able to help.

I've tried;

  • php artisan storage:link
  • storage_path()
  • public_path()
  • chmod -R 775 on the directory.

None of these have worked and I'm constantly greeted with the error; InvalidArgumentException with message 'Cannot write to directory "/public/storage/images"'.

My factory code is like so;

$factory->define(Product::class, function (Faker $faker)
{
    return [
        'id' => $faker->uuid,
        'name' => $faker->text(),
        'icon' => $faker->image(storage_path('images'), 64, 64, null, false)
    ];
});

Can anyone explain why this is happening?

0 likes
8 replies
ftiersch's avatar
ftiersch
Best Answer
Level 28

Does this error appear EVERYTIME or just sometimes?

Since you usually use factories inside of the console it could be, that the user executing the console command doesn't have the right to write in those directories. If you try chmod 777, does it work then? (if it does you should fix the user problem, not leave it at 777! :))

1 like
VertexBuffer's avatar

@ftiersch Interesting. I thought for sure performing chmod on the parent would have fixed it but I guess not! I was also under the impression Laravel automatically created the folder, but that doesn't seem to be the case.

I created the folder manually, did chmod 777 and that seems to have fixed the issue.

My question now is, what is the best place for me to put the code to initialise these folders when my Laravel app is deployed? E.G

$filepath = public_path('storage/images');

    if(!File::exists($filepath)){
        File::makeDirectory($filepath);
    }

Not really sure where the best file to put this would be lol.

Also, what is the general convention for this type of stuff? I noticed there is both public_path and storage_path, but I'm unsure as to which one I should be using. Since Laravel provides artisan storage:link, I'd assume they'd want you to use public_path('storage/<folder>') right?

ftiersch's avatar

Like I said, that is NOT the solution! You just enabled everyone to write to that directory!

But that shows us the actual problem: Your webserver can write to that directory (which is the www-data user usually) but when you login to that machine you use a different user. And then you execute artisan with that user and that user cannot write to your storage directory. You should fix that and not leave it at 777!

1 like
VertexBuffer's avatar

@ftiersch Can you give an example of how I would solve this? I'm not particularly that good when it comes to OS permissions.

Taking a guess, would chmod 755 be appropriate?

ftiersch's avatar

I think chmod 775 is appropriate and then add the user you are using to login to the www-data usergroup. Then it should work.

If you run the command ls -la you can see what user / group your storage directory belongs to. If you use chmod 775 those two can write into it. So you need to add your "login-user" to that group :)

1 like
VertexBuffer's avatar

@ftiersch Thanks I fixed that all up. However, I was still wondering the question I asked before which was; "My question now is, what is the best place for me to put the code to initialise these folders when my Laravel app is deployed? E.G"

$filepath = public_path('storage/images');

    if(!File::exists($filepath)){
        File::makeDirectory($filepath);
    }

Basically where would I put this code so that whenever I deploy or change something, it will create any missing directories that are required by my app so I don't need to create them manually?

ftiersch's avatar

That probably depends a little on your setup / deployment. Generally I would keep those directories in my git repository (with a .gitignore that ignores all the files inside so they are not committed accidentally) because that way the structure should be there for whoever uses it if you work in a team or something like that.

1 like

Please or to participate in this conversation.