PHILADELPHIA—Beneath a freight railway north of downtown, an estimated 200 people congregate in tents and atop mattresses in four dank tunnels. Many openly inject opioids into their hands, arms and necks.

The drug use spills out into the city’s row-house-filled Kensington neighborhood. On a recent sunny day, a gaunt man rocked in place on a nearby street, a syringe gripped sideways in his mouth, as three children walk by. Residents frequently find used syringes and say streets have become toilets.

Many...