Friends carried him to a hospital, where he said he was refused admission. Several months later, his wound still had not healed: He needs an operation on his leg but cannot afford one.

His disability prevented him from leaving Morocco. Instead, he settled in Rabat, where he was recently attacked by a gang, leaving him with multiple knife wounds on his hand, arm and chest. The attackers stole his cellphone and $100 that he had saved from begging on the streets.

“Nobody tried to help me, I was left for dead on the sidewalk,” he said. “African migrants get attacked because bandits know that we can’t go to the police — they will treat us even worse.”

Morocco expels an estimated 14,000 immigrants every year, according to Gadem, the antiracism group.

“Arrests of migrants, refugees and asylum seekers are based on racial profiling,” said Camille Denis, a coordinator at Gadem. “The time they spend in custody is not respected, and collective expulsions, prohibited by international and national law, are a common practice.”

Beside the illegal migrants, Morocco has also become home to thousands of legal immigrants from places like Senegal and Mali, said Mr. de Haas, the migration expert at Oxford. “Morocco has to deal with the reality that there are more migrants who are not just transiting but who are settling in the country,” he said.

Pierre Delagrange, president of the Collective of Sub-Saharan Communities in Morocco, a group formed to fight abuse of migrants, said that on the long journey from their homes, they were systematically robbed by the smugglers who took them across borders and threatened to abandon them unless they paid more money.

In Morocco, unable to go further, they often end up in a life of begging and prostitution or, worse, become the victims of human traffickers.