The driver of a truck had a lucky escape when his vehicle got stuck on a railway crossing and was then hit by a train.