0
/**
         * A function to update single image. Params are filename, name, directory, the image file, old images
         */
       static function singleUploadUpdate($filename, $directory, $file, $data){
            $dir = "backend/images/".$directory;
            $large_dir = $dir."/large";
            if (!empty($file)){
                if (!File::exists($dir)){
                    File::makeDirectory($dir, 0755, true);
                }
                if (!File::exists($large_dir)){
                    File::makeDirectory($large_dir, 0755, true);
                }
                $images = json_decode($data);
                foreach ($images as $key => $value){
                    File::delete("public/".$value->big_image);
                    File::delete("public/".$value->small_image);
                }
                $uploaded_file_name = $filename.rand(1,100000).".".$file->getClientOriginalExtension();
                $path = public_path($dir."/".$uploaded_file_name);
                $path2 = public_path($large_dir."/".$uploaded_file_name);
                Image::make($file->getRealPath())->save($path2);
                Image::make($file->getRealPath())->resize(250,250)->save($path);
                $files = [
                    "small_image" => $dir."/".$uploaded_file_name,
                    "big_image" => $dir."/large/".$uploaded_file_name
                ];
                return json_encode($files);
            }else{
                return $data;
            }
        }

this is my helper function to update user image, I got error on these lines :

 foreach ($images as $key => $value){

            File::delete("public/".$value->big_image);

            File::delete("public/".$value->small_image);

        }

and my image upload codes to create a user firt time is here:

/**
 * A function to upload single image. Params are filename, name, directory and a file
 */
static function singleUpload($filename, $directory, $file){
    $dir = "backend/images/".$directory;
    $large_dir = $dir."/large";
    if (!empty($file)){
        if (!File::exists($dir)){
            File::makeDirectory($dir, 0755, true);
        }
        if (!File::exists($large_dir)){
            File::makeDirectory($large_dir, 0755, true);
        }
        $uploaded_file_name = $filename.rand(1,100000).".".$file->getClientOriginalExtension();
        $path = public_path($dir."/".$uploaded_file_name);
        $path2 = public_path($large_dir."/".$uploaded_file_name);
        Image::make($file->getRealPath())->save($path2);
        Image::make($file->getRealPath())->resize(250,250)->save($path);
        $files = [
            "small_image" => $dir."/".$uploaded_file_name,
            "big_image" => $dir."/large/".$uploaded_file_name
        ];
        return json_encode($files);
    }else{
        return [];
    }
}

and I call this this functions like this

imageUpload::singleUploadUpdate(mHelper::permalink($request->job_title.rand(1,1000)), "users", $request->file("avatar"),$all_images);

so what I did wrong ? In blade template engine or somewhere else everything works fine, but while uploading image makes problem. Thank you for your help.

Tugrul Yildirim
  • 329
  • 2
  • 8
  • 1
    The code you posted appears to be unrelated to the error message in your question title (there is no code here that tries to access a property named "key"). The error message should include a file name and line number, can you indicate which line of code is triggering the error according to the error message? – rickdenhaan Feb 01 '22 at 16:11

0 Answers0