File system changes in App Engine 1.9.18

In the 1.9.18 App Engine release we added a two new filesystem features to help application developers. The first is a change to the local filesystem in the development server that makes it appear to be readonly, to match how the application works in production. The second is we’ve added an in memory virtual filesystem that makes it possible to create temporary files using sys_get_temp_dir and associated functions.

Development Server Read-Only File System

We notices that a lot of new developers were caught out by the fact that the local filesystem in production is read-only, so we added a feature to mimic this behavior in the development environment. Now, by default, if your application tries to write to the local filesystem you will see an error message similar to what is shown below.

>>> file_put_contents('foo.txt', 'ttt');
file_put_contents(foo.txt): failed to open stream: Read-only file system

We know there are scenarios where you want to be able to write to the local filesystem, for example if pre-caching data before you deploy the application to production. To make this possible, we’ve added a flag that you can set in your applications php.ini file to make the filesystem read/write. Note this flag only works in the development server, the local filesystem will always be readonly in production.

To make the filesystem read/write, add the following to your php.ini file.

google_app_engine.disable_readonly_filesystem = 1

Then you’ll be able to write to the local filesystem without restriction – just remember that the production filesystem is always read-only.

Memory backed virtual filesystem

In the new PHP 5.5 runtime You can now use sys_get_temp_dir(), tmpfile() and tempnam() functions to create temporary files.

The temporary files generated are stored in an in memory virtual filesystem. The paths will start with vfs:// and look something like

>>> echo tempnam('foo', 'bar');
>>> echo sys_get_temp_dir();

The in memory filesystem will be flushed at the end of the request, and is not shared in between requests to the same instance. We’ve made it possible to call rename() with a cloud storage path as the destination so that you can write out the temporary file for permanent storage.

$tmp_name = tempnam('foo', 'bar');
$gcs_file = 'gs://my_bucket/foo/bar.txt';

file_put_contents($tmp_name, "Hello World");
rename($tmp_name, $gcs_file);
echo file_get_contents($gcs_file);

If you run into any problems with either of these new features, please let us know by filing an issue.