Row locking or avoiding duplicate insertion on primarykey/otherfield combo
The OP answered their own question in the comments (now deleted):


  Never mind I figured it out, using a unique index combo, ALTER TABLE files ADD UNIQUE INDEX (file_relation, file_relation_id, file_dimension) does the trick.