While Cloud Storage
is the recommended solution for reading and writing files in App Engine,
if your app only needs to write temporary files, you can use standard
PHP 7 methods to write files to a directory named
All files in this directory are stored in the instance's RAM, therefore writing
/tmp takes up system memory. In addition, files in the
/tmp directory are
only available to the app instance that created the files. When the instance is
deleted, the temporary files are deleted.
tempnam(). This version of
tempnam()returns an in-memory temporary file that can be written to a permanent storage solution such as Cloud Storage buckets.
Here is an example on how to create an in-memory file with
tempnam(), and then
write to the file using
<?php $dir = sys_get_temp_dir(); $tmp = tempnam($dir, "foo"); file_put_contents($tmp, "hello"); $f = fopen($tmp, "a"); fwrite($f, " world"); fclose($f); echo file_get_contents($tmp);
This example will produce the following output: