Napoleon wanted Portugal to follow his Continental System, so he traveled through Spain to get there. After replacing Spain's leaders with his brother and stationing several troops around the country, Britain and Spain both allied together to fight Napoleon's army. At the end of the war in 1814, Napoleon was defeated and exiled from Spain, which seemed to be the first part of his eventual downfall.
THE PENINSULAR WAR. The war started when French and Spanish armies occupied Portugal in 1807, and escalated in 1808 when France turned on Spain, its ally until then. The war on the peninsula lasted until the Sixth Coalition defeated Napoleon in 1814, and is regarded as one of the first wars of national liberation, significant for the emergence of large-scale guerrilla warfare.