When the file doesn't exist, you are prepending
../ to the
path, thus you are trying to write to:
Are you sure that the folder
../../templates exists (and
that PHP can write to it)?
Before you write to a file, you need to check that the folder exists.