fsdolphin's avatar

Uploading images to the `public` directory stops working after the first image is uploaded

I'm trying to upload images to the publicfolder but for some reason it only works the first time I run my code, in other words as soon as the images folder is created inside the public directory it stops working and I get a Not Found message. The first image is uploaded and saved to the database, but again, it only works one time and if I delete the images folder it works again for another image and then stops working again.

Message

Not Found:

The requested resource /images was not found on this server.

FYI - This is what I see in the browser when I get the message http://localhost:8000/images

Here is the code I have...

Routes:

Route::post('images', 'ImageController@store');

Controller:

class ImageController extends Controller{
        public function store(){
            $file = request()->file('image');
            $fileName = str_replace(' ','', $file->getClientOriginalName());
            $file->move(public_path().'/images/', $fileName);

            // Save to database
            $image = new \App\Image();
            $image->imageName = $fileName;
            $image->save();

            return back();
        }
}

Form:

<body>

    <form method="POST" action="images" enctype="multipart/form-data">

        {{ csrf_field() }}

        <input type="file" name="image">

        <button type="submit">Save</button>

    </form>

</body>

The funny thing is that if I change my code to upload to the storage directory instead it works fine, but of course I want to upload to the public directory.

If I change...

$file->move(public_path().'/images/', $fileName);

to

$file->storeAs('images/', $fileName);

It works fine.

What am I doing wrong?

0 likes
8 replies
jlrdw's avatar

Move should work, are you sure it is a different file?

1 like
fsdolphin's avatar

Yes, I'm trying to upload a different image the second time but I keep getting the error. I just confirmed it and still got the Not Found message.

FYI - Also, the second time I try it doesn't save to the database either.

jlrdw's avatar

Here is some older code I have used

if (isset($_POST['submit'])) {
            $lid = DB::table('dogs')->count();
            $lid = $lid + 1;
            $file = Input::file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];

            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ROOTDIR . 'upload/imgdogs/';
            $file->move($destinationPath, $newname);

            $dogpic = Cln::fixValue($newname);
            $dogname = ucfirst(Cln::fixValue($_POST['dogname']));
            $sex = ucfirst($_POST['sex']);
            $comments = Cln::fixValue($_POST['comments']);
            $adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');
            $lastedit = date("Y-m-d H:i:s");
            
            if (!isset($error)) {
                $postdata = array(
                    'dogpic' => $dogpic,
                    'dogname' => $dogname,
                    'sex' => $sex,
                    'comments' => $comments,
                    'adopted' => $adopted,
                    'lastedit' => $lastedit
                );

                DB::table('dogs')->insert($postdata);
            }
        }

ROOTDIR is a constant I created

See if some of my mess helps any.

1 like
fsdolphin's avatar

Thanks, I will see if I can make this thing works.

jlrdw's avatar

Just make sure you get paths right, and permissions are right.

Snapey's avatar
Snapey
Best Answer
Level 122

You cannot have a folder with the same name as a route

The path /images goes to your controller, but then you create a folder called /images in public.

Next request comes in, .htaccess sees a folder called /images and instead of sending the request to laravel it looks for index.html, default.html etc in your images folder.

1 like
fsdolphin's avatar

@Snapey, you are right, I changed the folder name and it now works. Thanks a lot for your help.

$file->move(public_path().'/pictures/', $fileName);

FYI - I will probably change the route name instead not the folder name.

fsdolphin's avatar

@Snapey Could you please elaborate a little bit more on why it was redirected to the images folder instead of responding to the POST request the second time?

Is this a normal behavior with http requests where by default if a folder with the same name as the request is found the POST request will be ignored, meaning that folders have precedence?

I know this is not related to my issue but I would like to have a better understanding. Thanks a lot.

Please or to participate in this conversation.