Skip to content
Advertisement

“SplFileInfo::getSize(): stat failed” exception on Laravel 6.0 when doing file upload

I’m using Laravel 6.0 to build a simple image upload system.

But for some odd reason, I keep encountering a “SplFileInfo::getSize(): stat failed” exception even though the image is successfully uploaded to the correct folder… is there something I can do to either 1) skip this exception and hide it 2) fix it entirely?

I did see a solution to change max_file_size and post_max_size etc etc to a higher value and restart Apache. I tried that but it didn’t work either… and it looks like there aren’t any clear cut solutions for this.

Here is what I have on my controller for the upload function

public function fileUpload(Request $request) {
        $this->validate($request, [
            'img' => 'required|image|mimes:jpeg,png,jpg,',
        ]);

        if ($request->hasFile('img')) {
            $image = $request->file('img');
            $size = $image->getSize();
            $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $charactersLength = strlen($characters);
            $randomString = '';
            for ($i = 0; $i < 5; $i++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)]; // Generate random 5 character name...
            }
            $name = $randomString.'.'.$image->getClientOriginalExtension();
            $destinationPath = public_path('storage'.'\'.$request->input('post'));

            // Store Image Information //
            $data = new Photo();
            // Get post number //
            $data->order = $request->input('post');
            $data->filename = $name;
            $data->location = $destinationPath;
            $data->save();

            // Now move to the destination path //
            $image->move($destinationPath, $name);

            return refresh();
        }
    }

I’m using MySQL to store photo information such as which post the image is associated with, the file name generated by the controller, and where in the machine the image is being stored… as you can see, I am creating separate folders for each post for organization purposes…

The goal is to at least disable the exception cause otherwise the whole thing works fine… I should theoretically just refresh the page once the upload is done.

Advertisement

Answer

Guys, I figured it out…

There might need to be a minor update in Laravel to address this issue… because it’s an odd “bug”.

SO, apparently, you MUST do a getSize();, in my case $size = $image->getSize(); and store that value somewhere whether it be on a SQL database… even if you don’t need the data… simply storing it as a variable will throw the error.

Once I did return $size, the error disappeared and returned the bytes value of the image size… When I stored it as a mySQL value in a column, the issue wasn’t present again…

I hope this helps someone. lol. If you encounter this issue and you don’t understand, I’ll be happy to explain. 🙂

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement