The Messenger of Allah ﷺ saw some phlegm on the wall of the mosque, so he picked up a pebble and scraped it off. Then he said, "When one of you needs to spit, let him not spit in front of him, nor to his right. Let him spit to his left, or beneath his left foot."
إن رسول الله ﷺ رأى نخامة في جدار المسجد، فتناول حصاة فحكها فقال: إذا تنخم أحدكم فلا يتنخمن قبل وجهه، ولا عن يمينه، ليبصق عن يساره، أو تحت قدمه اليسرى.
Bukhari narrated it in the Book of Prayer (408, 409), and Muslim in the Book of Masjids (548), both from the hadith of Ibrahim bin Saad, from Ibn Shihab, from Humayd bin Abdul-Rahman, that Abu Hurairah and Abu Saeed both told him the account, and they mentioned the hadith. The wording given here is Bukhari's. As for Muslim, he did not give the wording of the hadith of Ibrahim bin Saad, but instead referred to the wording of the hadith of Sufyan bin Uyayna, from Zuhri, from Humayd, from Abu Saeed Khudri alone, that the Prophet ﷺ saw phlegm on the wall in the direction of the qiblah in the masjid, so he scraped it off with a pebble, then forbade a man from spitting to his right or in front of him, but instead to spit to his left or under his foot.