Spin systems and harmonic oscillators comprise two archetypes in quantum mechanics. The spin-1/2 system, with two quantum energy levels, is essentially the most nonlinear system found in nature, whereas the harmonic oscillator represents the most linear, with an infinite number of evenly spaced quantum levels. A significant difference between these systems is that a two-level spin can be prepared in an arbitrary quantum state using classical excitations, whereas classical excitations applied to an oscillator generate a coherent state, nearly indistinguishable from a classical state. Quantum behaviour in an oscillator is most obvious in Fock states, which are states with specific numbers of energy quanta, but such states are hard to create. Here we demonstrate the controlled generation of multi-photon Fock states in a solid-state system. We use a superconducting phase qubit, which is a close approximation to a two-level spin system, coupled to a microwave resonator, which acts as a harmonic oscillator, to prepare and analyse pure Fock states with up to six photons. We contrast the Fock states with coherent states generated using classical pulses applied directly to the resonator.