Write empty files if the POST data is empty / missing; also *peek* instead of reading! (we could, of course, read, then seek, but peek is simpler).
Small filename / dirname fix.