Laravel error uploading image using $file->move()

35 views Asked by At

My uploading function:

public function uploadImage($file, $path) {
    $filename = Str::random(6) . '_' . time() . '_' . $file->getClientOriginalName();
    $uploads_folder = public_path() . $path;
    if (!file_exists($uploads_folder)) {
        mkdir($uploads_folder, 0777, true);
    }
    $image = $path . "/" . $filename;

    $file->move($uploads_folder, $filename);
    return $image;
}

It worked fine for the past weeks but now this exception appeared:: Unsupported operand types the return statement didn't apply but the file is saved on the folder correctly.

1

There are 1 answers

0
Omar Chouman On

Maybe it's an issue with the $path variable that you're using to construct the image and the unsupported operand types is probably because the $path is not outputting a String.

To make sure that it is really a string you can use the strval around the path like so:

$image = strval($path) . "/" . $filename;