If the flap at the end of the tunnel is indestructible, then yes, the train will get trapped in the tunnel. Though it will be completely destroyed.
What happens when the front of the train hits the flap is that it comes to a sudden halt. But the rest of the train is still moving. It has to still be moving, since the fastest any signal can propagate is the speed of light. So the train will crumple like an accordion.
If you do the math in the original frame of the train, you'll see that the back of the train always makes it into the tunnel before that time has elapsed.
The end result of this is that the train ends at rest, with a length of at most $x$ meters.
A natural question to ask at this point is "what if the train is rigid and indestructible?" The real answer to this is "it can't be." There is no such thing as a perfectly rigid object in special relativity- the same reason you can't send faster-than-light signals just by pushing a really long pole.