I found myself looking this up, I’ve probably done that before.
To write a file in PHP:
$path = './test.txt';
$file = fopen( $path, 'w' );
if ( ! $file ) { throw new Exception( "Cannot write '$file'." ); }
fwrite( $file, "content\n" );
fclose( $file );