They both reported that the Messenger of Allah ﷺ saw some phlegm on the wall of the mosque. He took a pebble and scraped it off, then said, "When one of you needs to expel phlegm, let him not spit in front of his face nor to his right. Let him spit to his left or under his left foot."
حدثنا محمد بن عثمان العثماني أبو مروان حدثنا إبراهيم بن سعد عن ابن شهاب عن حميد بن عبد الرحمن بن عوف
أنهما أخبراه أن رسول الله ﷺ رأى نخامة في جدار المسجد فتناول حصاة فحكها ثم قال إذا تنخم أحدكم فلا يتنخمن قبل وجهه ولا عن يمينه وليبزق عن شماله أو تحت قدمه اليسرى
Bukhari narrated it in "Prayer," in the chapter on scraping off mucus with a pebble from the masjid, hadith 405, from hadith 408 and 409 and elsewhere, and Muslim in "Masjids," in the chapter on the prohibition of spitting in the masjid, through the hadith of Ibrahim bin Saad and others, with this wording.