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

hjortur17's avatar

Can't write image data to path

Hi, I'm trying to store images to the public folder, but it's telling me that I can't write image data to path. Please help! Here is my code:

if (request()->hasFile('image')) {
                     $image = request()->file('image');
                     $filename = time() . '.' . $image;
                     $path = public_path('images/' . $filename);

                     // if (!file_exists($path)) {
                     //        mkdir($path, 666, true);
                     // }

                     Image::make($image)->resize(128, 128)->encode('jpg', 75)->save($path);

                     Election::create([
                            'image' => $filename
                     ]);
              }
                        

I have tried to do chmod -R 777 public/images/ but that doesn't work

0 likes
2 replies
Cronix's avatar
$filename = time() . '.' . $image;

What does that come out to? $image is the image object that you got from the request in the line above it, not the name of the image...

hjortur17's avatar

This was suppost to be like this:

$filename = time() . '.' . $image->getClientOriginalExtension();

Please or to participate in this conversation.